前言
華羅庚說過
數(shù)缺形時(shí)少直觀,形少數(shù)時(shí)難入微.
這句話第一次聽還是初中數(shù)學(xué)老師上二次方程課時(shí)說的.最近看到了3blue1brown對線性代數(shù)的直觀解釋感覺豁然開朗,于是我又撿起了兒時(shí)對美妙數(shù)學(xué)的興趣. 發(fā)現(xiàn)一個(gè)博客,數(shù)據(jù)可視化很好的例子,決定花些時(shí)間和大家一起解讀一下
例程來自:https://www.machinelearningplus.com/plots/matplotlib-histogram-python-examples//
感謝b站UP "菜菜TsaiTsai" 分享這個(gè)博客.
簡單回顧下上期內(nèi)容葱色,我們的目標(biāo)是在一張圖上展示多個(gè)維度信息,例如購買口紅時(shí)我們會參考他的銷量痕钢、色號挑童、價(jià)格等等參數(shù),當(dāng)我們把每個(gè)品牌的口紅個(gè)個(gè)參數(shù)都體現(xiàn)在一張圖上時(shí)這些點(diǎn)會有某種分布的趨勢糕非。這個(gè)趨勢在總樣本越多時(shí)越明顯蒙具。這期我們要綜合之前的散點(diǎn)圖和直方圖來展示。
例7
# Import Data
df = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv")
# Create Fig and gridspec
fig = plt.figure(figsize=(16, 10), dpi= 80)
grid = plt.GridSpec(4, 4, hspace=0.5, wspace=0.2)
# Define the axes
ax_main = fig.add_subplot(grid[:-1, :-1])
ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])
ax_bottom = fig.add_subplot(grid[-1, 0:-1], xticklabels=[], yticklabels=[])
# Scatterplot on main ax
ax_main.scatter('displ', 'hwy', s=df.cty*4, c=df.manufacturer.astype('category').cat.codes, alpha=.9, data=df, cmap="tab10", edgecolors='gray', linewidths=.5)
# histogram on the right
ax_bottom.hist(df.displ, 40, histtype='stepfilled', orientation='vertical', color='deeppink')
ax_bottom.invert_yaxis()
# histogram in the bottom
ax_right.hist(df.hwy, 40, histtype='stepfilled', orientation='horizontal', color='deeppink')
# Decorations
ax_main.set(title='Scatterplot with Histograms \n displ vs hwy', xlabel='displ', ylabel='hwy')
ax_main.title.set_fontsize(20)
for item in ([ax_main.xaxis.label, ax_main.yaxis.label] + ax_main.get_xticklabels() + ax_main.get_yticklabels()):
item.set_fontsize(14)
xlabels = ax_main.get_xticks().tolist()
ax_main.set_xticklabels(xlabels)
plt.show()
解析
我們上期提過一個(gè)問題朽肥,在一個(gè)平面上如何展示超過3個(gè)特征的維度信息禁筏。當(dāng)時(shí)的答案有,x衡招,y坐標(biāo)篱昔,等高線法,標(biāo)記顏色。上例采用了一個(gè)更簡單暴力的方法:再畫一個(gè)圖州刽。
解析下代碼流程
- 導(dǎo)入數(shù)據(jù)表
- 分割畫布
- 設(shè)置畫布尺寸空执,和其他2個(gè)畫布尺寸
- 畫圖,需要畫1幅散點(diǎn)圖穗椅,2幅直方圖
- 標(biāo)注 圖表的頭和軸的名字等等信息
方法參數(shù)解釋
- plt.GridSpec(4, 4辨绊,hspace=0.5, wspace=0.2) 把畫布分為 4x4 高為0.5單位,寬為0.2單位
- ax_bottom.invert_yaxis() 把y 軸作為很水平軸
圖像
應(yīng)用
這里我們學(xué)到一個(gè)圖形分割的方法匹表。把一個(gè)長方形分成4分门坷,去3x3面積作為主畫面,剩下的1x4和4x1 大小作為輔助的畫面袍镀。你可以找一張紙畫畫看拜鹤,這讓我想起小學(xué)時(shí)做的那些數(shù)學(xué)題。來看圖像這次我們有更加量化的圖像流椒,但還是不夠敏簿。因?yàn)榧尤胱屇銇砻枋鲞@張圖表,表示什么宣虾,你可能很難回答惯裕。我們需要把具體再回到概括,不過這次用一個(gè)數(shù)值來概括绣硝◎呤疲看下一個(gè)例子
例8
# Create Fig and gridspec
fig = plt.figure(figsize=(16, 10), dpi= 80)
grid = plt.GridSpec(4, 4, hspace=0.5, wspace=0.2)
# Define the axes
ax_main = fig.add_subplot(grid[:-1, :-1])
ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])
ax_bottom = fig.add_subplot(grid[-1, 0:-1], xticklabels=[], yticklabels=[])
# Scatterplot on main ax
ax_main.scatter('displ', 'hwy', s=df.cty*5, c=df.manufacturer.astype('category').cat.codes, alpha=.9, data=df, cmap="Set1", edgecolors='black', linewidths=.5)
# Add a graph in each part
sns.boxplot(df.hwy, ax=ax_right, orient="v")
sns.boxplot(df.displ, ax=ax_bottom, orient="h")
# Decorations ------------------
# Remove x axis name for the boxplot
ax_bottom.set(xlabel='')
ax_right.set(ylabel='')
# Main Title, Xlabel and YLabel
ax_main.set(title='Scatterplot with Histograms \n displ vs hwy', xlabel='displ', ylabel='hwy')
# Set font size of different components
ax_main.title.set_fontsize(20)
for item in ([ax_main.xaxis.label, ax_main.yaxis.label] + ax_main.get_xticklabels() + ax_main.get_yticklabels()):
item.set_fontsize(14)
plt.show()
解析
這是第二次用到 seabron 包,他除了配色好看鹉胖,還直接幫你計(jì)算了握玛,中值、回歸線這些東西
解析下代碼流程
- 分割畫布
- 設(shè)置畫布大小
- 畫圖甫菠,一幅散點(diǎn)圖挠铲,2幅箱圖
- 添加標(biāo)注
方法參數(shù)解釋
sns.boxplot(df.hwy, ax=ax_right, orient="v")
- 輸入為一個(gè)一維數(shù)列。
- ax 可以用 matplotlib 的畫布作為輸入畫布
- orient 設(shè)置方向
圖像
應(yīng)用
再箱圖中的橫線就是這個(gè)樣本的中位數(shù)寂诱,現(xiàn)在我們可以描述這個(gè)圖了拂苹,市場上主流排量車型時(shí)3.2L,主流的油耗在每英里25加侖痰洒。
下期預(yù)告
下期我們將做一次總結(jié)瓢棒,不在介紹新的統(tǒng)計(jì)圖。事實(shí)上對于非專業(yè)統(tǒng)計(jì)員而言丘喻,散點(diǎn)圖脯宿,折線圖,直方圖泉粉,箱圖连霉,已經(jīng)能應(yīng)對生活中99.9%場景了。這次總結(jié)我們會完整且詳細(xì)的把一個(gè)表格變成一幅漂亮而且有意義的圖表。
下期預(yù)告:統(tǒng)計(jì)50圖——總結(jié)與寶可夢游戲數(shù)值統(tǒng)計(jì)