Python Beginners(7) - DataViz2

Add legend

plt.plot(women_degrees['Year'], women_degrees['Biology'], c='blue')
plt.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green')
plt.title("Percentage of Biology Degrees Awarded By Gender")
plt.legend(['Women', 'Men'], loc = "upper right")
plt.show()

Using ax.plot and turndown tick_params

fig, ax = plt.subplots()
ax.plot(women_degrees['Year'], women_degrees['Biology'], c='blue')
ax.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green')
ax.set_title("Percentage of Biology Degrees Awarded By Gender")
ax.legend(['Women', 'Men'], loc = "upper right")
ax.tick_params(bottom="off", top="off", left="off", right="off")
slist = ax.spines  # remove all spines
for item in slist:
    ax.spines[item].set_visible(False)
plt.show()

Change color of lines
Set color like: some_color = (12/255, 10/255, 103/255)
then set some_color to the line.

fig = plt.figure(figsize=(12, 12))
dark_blue = (0/255, 107/255, 164/255) # set color
orange = (255/255, 128/255, 14/255) # set color
for sp in range(0,4):
    ax = fig.add_subplot(2,2,sp+1)
    # The color for each line is assigned here.
    ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c=dark_blue, label='Women', linewidth = 3)
    ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c=orange, label='Men', linewidth = 3)
    for key,spine in ax.spines.items():
        spine.set_visible(False)
    ax.set_xlim(1968, 2011)
    ax.set_ylim(0,100)
    ax.set_title(major_cats[sp])
    ax.tick_params(bottom="off", top="off", left="off", right="off")
plt.legend(loc='upper right')
plt.show()

Make charts in a row

stem_cats = ['Engineering', 'Computer Science', 'Psychology', 'Biology', 'Physical Sciences', 'Math and Statistics']
fig = plt.figure(figsize=(18, 3))
for sp in range(0,6):
    ax = fig.add_subplot(1,6,sp+1)
    ax.plot(women_degrees['Year'], women_degrees[stem_cats[sp]], c=cb_dark_blue, label='Women', linewidth=3)
    ax.plot(women_degrees['Year'], 100-women_degrees[stem_cats[sp]], c=cb_orange, label='Men', linewidth=3)
    for key,spine in ax.spines.items():
        spine.set_visible(False)
    ax.set_xlim(1968, 2011)
    ax.set_ylim(0,100)
    ax.set_title(stem_cats[sp])
    ax.tick_params(bottom="off", top="off", left="off", right="off")
plt.legend(loc='upper right')
plt.show()

Add annotations

fig = plt.figure(figsize=(18, 3))
for sp in range(0,6):
    ax = fig.add_subplot(1,6,sp+1)
    if sp == 0:
        ax.text(2005, 87, "Men")
        ax.text(2002, 8, "Women")
    elif sp == 5:
        ax.text(2005, 62, "Men")
        ax.text(2001, 35, "Women")
    else:
        pass
    ax.plot(women_degrees['Year'], women_degrees[stem_cats[sp]], c=cb_dark_blue, label='Women', linewidth=3)
    ax.plot(women_degrees['Year'], 100-women_degrees[stem_cats[sp]], c=cb_orange, label='Men', linewidth=3)
    for key,spine in ax.spines.items():
        spine.set_visible(False)
    ax.set_xlim(1968, 2011)
    ax.set_ylim(0,100)
    ax.set_title(stem_cats[sp])
    ax.tick_params(bottom="off", top="off", left="off", right="off")
plt.legend(loc='upper right')
plt.show()

Another Exercise - Titanic

import pandas as pd
titanic = pd.read_csv("train.csv")
titanic = titanic[['Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']]  # select only the columns we want
print (titanic.head(5))
titanic = titanic.dropna(axis=0, how='any')  # drops rows where there is any NaN

Seaborn with kernal density estimation

import seaborn as sns
import matplotlib.pyplot as plt
sns.distplot(titanic["Age"])
plt.show()

Draw the KDE line ONLY

sns.kdeplot(titanic["Age"], shade = True)
plt.xlabel("Age")

sns tweaking

sns.set_style("white")
sns.kdeplot(titanic["Age"], shade = True)
plt.xlabel("Age")
sns.despine(left = True, bottom=True)

Small Multiple

# Condition on unique values of the "Survived" column.
g = sns.FacetGrid(titanic, col="Survived", size=6)
# For each subset of values, generate a kernel density plot of the "Age" columns.
g.map(sns.kdeplot, "Age", shade=True)

OR

g = sns.FacetGrid(titanic, col = 'Pclass', size = 6) # here 6 means 6 inches in height
g.map(sns.distplot, "Age")
sns.despine(left = True, bottom = True)
plt.show()

para "hue, add legend"
doc

g = sns.FacetGrid(titanic, col="Survived", row="Pclass", hue = "Sex", size = 3) 
g.map(sns.kdeplot, "Age", shade = True).add_legend() # add legend
sns.despine(left=True, bottom=True)
plt.show()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末猛拴,一起剝皮案震驚了整個濱河市横堡,隨后出現(xiàn)的幾起案子泻红,更是在濱河造成了極大的恐慌硕蛹,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件韩脏,死亡現(xiàn)場離奇詭異该面,居然都是意外死亡,警方通過查閱死者的電腦和手機奈附,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來煮剧,“玉大人斥滤,你說我怎么就攤上這事讼载。” “怎么了中跌?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長菇篡。 經(jīng)常有香客問我漩符,道長,這世上最難降的妖魔是什么驱还? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任嗜暴,我火速辦了婚禮,結(jié)果婚禮上议蟆,老公的妹妹穿的比我還像新娘闷沥。我一直安慰自己,他們只是感情好咐容,可當(dāng)我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布舆逃。 她就那樣靜靜地躺著,像睡著了一般戳粒。 火紅的嫁衣襯著肌膚如雪路狮。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天蔚约,我揣著相機與錄音奄妨,去河邊找鬼。 笑死苹祟,一個胖子當(dāng)著我的面吹牛砸抛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播树枫,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼直焙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了砂轻?” 一聲冷哼從身側(cè)響起箕般,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎舔清,沒想到半個月后丝里,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡体谒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年杯聚,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片抒痒。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡幌绍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情傀广,我是刑警寧澤颁独,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站伪冰,受9級特大地震影響誓酒,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜贮聂,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一靠柑、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧吓懈,春花似錦歼冰、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至甘穿,卻和暖如春畔勤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背扒磁。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工庆揪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人妨托。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓缸榛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親兰伤。 傳聞我的和親對象是個殘疾皇子内颗,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容