![用Pandas作圖_第1頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-1/25/5283ef13-b025-4fff-a8e8-63d899a1adef/5283ef13-b025-4fff-a8e8-63d899a1adef1.gif)
![用Pandas作圖_第2頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-1/25/5283ef13-b025-4fff-a8e8-63d899a1adef/5283ef13-b025-4fff-a8e8-63d899a1adef2.gif)
![用Pandas作圖_第3頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-1/25/5283ef13-b025-4fff-a8e8-63d899a1adef/5283ef13-b025-4fff-a8e8-63d899a1adef3.gif)
![用Pandas作圖_第4頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-1/25/5283ef13-b025-4fff-a8e8-63d899a1adef/5283ef13-b025-4fff-a8e8-63d899a1adef4.gif)
![用Pandas作圖_第5頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-1/25/5283ef13-b025-4fff-a8e8-63d899a1adef/5283ef13-b025-4fff-a8e8-63d899a1adef5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、 用Pandas作圖分類: python2014-07-11 12:20 32人閱讀 評(píng)論(0) 收藏 舉報(bào)目錄(?)+來自:/python/2014/02/23/Plotting_with_Pandas/#wat_e_12612920-6fe4-464e-a2b0-3b1f13c1a4f6_zss_關(guān)于Pandas的基本使用介紹,請(qǐng)查看另一篇博文:Python中的結(jié)構(gòu)化數(shù)據(jù)分析利器-Pandas簡(jiǎn)介推薦使用ipython的pylab模式,如果要在ipython notebook中嵌入圖片,則還需要指定
2、pylab=inline。ipython -pylab #ipython的pylab模式ipython notebook -pylab=inline #notebook的inline模式import pandas as pd基本畫圖命令Pandas通過整合matplotlib的相關(guān)功能實(shí)現(xiàn)了基于DataFrame的一些 作圖功能。下面的數(shù)據(jù)是每年美國(guó)男女出生數(shù)據(jù):url = 'present = pd.read_table(url, sep=' ')present.shape(63, 3)present.columnsIndex(u'year', u
3、39;boys', u'girls', dtype='object')可以看到這個(gè)數(shù)據(jù)集共有63條記錄,共有三個(gè)字段:Year,boys,girls。為了簡(jiǎn)化計(jì)算將year作為索引。present_year = present.set_index('year')plot是畫圖的最主要方法,Series和DataFrame都有plot方法。可以這樣看一下男生出生比例的趨勢(shì)圖:present_year'boys'.plot()plt.legend(loc='best')<matplotlib.legend
4、.Legend at 0x10b9c7610>這是Series上的plot方法,通過DataFrame的plot方法,你可以將男生和女生出生數(shù)量的趨勢(shì)圖畫在一起。present_year.plot()<matplotlib.axes.AxesSubplot at 0x108ce4910>present_year.girls.plot(color='g')present_year.boys.plot(color='b')plt.legend(loc='best')<matplotlib.legend.Legend at 0x
5、10999e510>可以看到DataFrame提供plot方法與在多個(gè)Series調(diào)用多次plot方法的效果是一致。present_year:10.plot(kind='bar')<matplotlib.axes.AxesSubplot at 0x10ab31390>plot默認(rèn)生成是曲線圖,你可以通過kind參數(shù)生成其他的圖形,可選的值為:line, bar, barh, kde, density, scatter。present_year:10.plot(kind='bar')<matplotlib.axes.AxesSubplot
6、at 0x10bb35890>present_year:10.plot(kind='barh')<matplotlib.axes.AxesSubplot at 0x10eb01890>如果你需要累積的柱狀圖,則只需要指定stacked=True。present_year:10.plot(kind='bar', stacked=True)<matplotlib.axes.AxesSubplot at 0x10bbdb3d0>制作相對(duì)的累積柱狀圖,需要一點(diǎn)小技巧。首先需要計(jì)算每一行的匯總值,可以在DataFrame上直接調(diào)用sum方法,
7、參數(shù)為1,表示計(jì)算行的匯總。默認(rèn)為0,表示計(jì)算列的匯總。present_year.sum(1):5year1940 23603991941 25134271942 28089961943 29368601944 2794800dtype: int64有了每一行的匯總值之后,再用每個(gè)元素除以對(duì)應(yīng)行的匯總值就可以得出需要的數(shù)據(jù)。這里可以使用DataFrame的div函數(shù),同樣要指定axis的值為0。present_year.div(present_year.sum(1),axis=0):10.plot(kind='barh', stacked=True)<matplotlib
8、.axes.AxesSubplot at 0x113223290>散點(diǎn)圖和相關(guān)plot也可以畫出散點(diǎn)圖。使用kind='scatter', x和y指定x軸和y軸使用的字段。present_year.plot(x='boys', y='girls', kind='scatter')<matplotlib.axes.AxesSubplot at 0x1141c9810>再來載入一下鳶尾花數(shù)據(jù)。url_2 = 'iris = pd.read_csv(url_2)iris.head(5) SepalLe
9、ngthSepalWidthPetalLengthPetalWidthName00.2Iris-setosa14.93.01.40.2Iris-setosa0.2Iris-setosa0.2Iris-setosa45.0Iris-setosa5 rows × 5 columnsiris.corr() SepalLengthSepalWidthPetalLengthPetalWidthSepalLength1.000000-0.1093690.8717540.817954SepalWidth-0.10
10、93691.000000-0.420516-0.356544PetalLength0.871754-0.4205161.0000000.962757PetalWidth0.817954-0.3565440.9627571.0000004 rows × 4 columnsfrom pandas.tools.plotting import scatter_matrixscatter_matrix(iris, alpha=0.2, figsize=(6, 6), diagonal='kde')array(<matplotlib.axes.AxesSubplot obj
11、ect at 0x1141e5290>, <matplotlib.axes.AxesSubplot object at 0x114313610>, <matplotlib.axes.AxesSubplot object at 0x11433fbd0>, <matplotlib.axes.AxesSubplot object at 0x114328e10>, <matplotlib.axes.AxesSubplot object at 0x11411f350>, <matplotlib.axes.AxesSubplot object a
12、t 0x114198690>, <matplotlib.axes.AxesSubplot object at 0x114181b90>, <matplotlib.axes.AxesSubplot object at 0x11436eb90>, <matplotlib.axes.AxesSubplot object at 0x11438ced0>, <matplotlib.axes.AxesSubplot object at 0x114378310>, <matplotlib.axes.AxesSubplot object at 0x1
13、143e34d0>, <matplotlib.axes.AxesSubplot object at 0x114d0a810>, <matplotlib.axes.AxesSubplot object at 0x1143ecd50>, <matplotlib.axes.AxesSubplot object at 0x114d40e90>, <matplotlib.axes.AxesSubplot object at 0x114d63210>, <matplotlib.axes.AxesSubplot object at 0x114d4a
14、2d0>, dtype=object)箱圖DataFrame提供了boxplot方法可以用來畫箱圖。iris.boxplot()'boxes': <matplotlib.lines.Line2D at 0x1141439d0>, <matplotlib.lines.Line2D at 0x11416c1d0>, <matplotlib.lines.Line2D at 0x1141559d0>, <matplotlib.lines.Line2D at 0x11414b210>, 'caps': <matp
15、lotlib.lines.Line2D at 0x11416af90>, <matplotlib.lines.Line2D at 0x1141434d0>, <matplotlib.lines.Line2D at 0x114172790>, <matplotlib.lines.Line2D at 0x114172c90>, <matplotlib.lines.Line2D at 0x114153f90>, <matplotlib.lines.Line2D at 0x1141554d0>, <matplotlib.lines
16、.Line2D at 0x11414f7d0>, <matplotlib.lines.Line2D at 0x11414fcd0>, 'fliers': <matplotlib.lines.Line2D at 0x114145410>, <matplotlib.lines.Line2D at 0x114145b50>, <matplotlib.lines.Line2D at 0x11416cbd0>, <matplotlib.lines.Line2D at 0x1141530d0>, <matplotlib
17、.lines.Line2D at 0x114151410>, <matplotlib.lines.Line2D at 0x114151b90>, <matplotlib.lines.Line2D at 0x11414bc10>, <matplotlib.lines.Line2D at 0x1141743d0>, 'medians': <matplotlib.lines.Line2D at 0x114143ed0>, <matplotlib.lines.Line2D at 0x11416c6d0>, <mat
18、plotlib.lines.Line2D at 0x114155ed0>, <matplotlib.lines.Line2D at 0x11414b710>, 'whiskers': <matplotlib.lines.Line2D at 0x11416a7d0>, <matplotlib.lines.Line2D at 0x11416aa10>, <matplotlib.lines.Line2D at 0x114172050>, <matplotlib.lines.Line2D at 0x114172290>,
19、 <matplotlib.lines.Line2D at 0x114153590>, <matplotlib.lines.Line2D at 0x114153a90>, <matplotlib.lines.Line2D at 0x11414f090>, <matplotlib.lines.Line2D at 0x11414f2d0>通過by參數(shù)可以計(jì)算不同分組情況下,各個(gè)字段的箱圖。iris.boxplot(by='Name', figsize=(8, 8)array(<matplotlib.axes.AxesSubplot object at 0x120dd8f50>, <matplotlib.axes.AxesSubplot object at 0x1218d3410>, <matplotlib.axes.AxesSubplot object at 0x1218f47d0>, <matplotlib.axes.AxesSubplot object at 0x
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 五年級(jí)下冊(cè)數(shù)學(xué)聽評(píng)課記錄《 分?jǐn)?shù)加減法簡(jiǎn)便運(yùn)算》人教新課標(biāo)
- 八年級(jí)道德與法治下冊(cè)第二單元理解權(quán)利義務(wù)第四課公民義務(wù)第二框依法履行義務(wù)聽課評(píng)課記錄(新人教版)
- 湘教版數(shù)學(xué)九年級(jí)上冊(cè)《4.4解直角三角形的應(yīng)用(1)》聽評(píng)課記錄
- 人教版歷史八年級(jí)下冊(cè)第15課《鋼鐵長(zhǎng)城》聽課評(píng)課記錄
- 天天練習(xí)-四年級(jí)上冊(cè)口算練習(xí)
- 七年級(jí)下學(xué)期語(yǔ)文教學(xué)工作總結(jié)
- 蘇教版小學(xué)數(shù)學(xué)三年級(jí)上冊(cè)口算試題全套
- 蘇教版四年級(jí)數(shù)學(xué)下冊(cè)期末復(fù)習(xí)口算練習(xí)題三
- 滬科版八年級(jí)數(shù)學(xué)下冊(cè)聽評(píng)課記錄《第17章一元二次方程數(shù)17.2一元二次方程的解法(第3課時(shí))》
- LED屏幕安裝協(xié)議書范本
- 華為攜手深圳國(guó)際會(huì)展中心創(chuàng)建世界一流展館
- 2023版思想道德與法治專題2 領(lǐng)悟人生真諦 把握人生方向 第3講 創(chuàng)造有意義的人生
- 全過程工程咨詢服務(wù)技術(shù)方案
- 小報(bào):人工智能科技科學(xué)小報(bào)手抄報(bào)電子小報(bào)word小報(bào)
- GB/T 41509-2022綠色制造干式切削工藝性能評(píng)價(jià)規(guī)范
- 企業(yè)生產(chǎn)現(xiàn)場(chǎng)6S管理知識(shí)培訓(xùn)課件
- 五年級(jí)下冊(cè)數(shù)學(xué)課件 第10課時(shí) 練習(xí)課 蘇教版(共11張PPT)
- 三年級(jí)道德與法治下冊(cè)我是獨(dú)特的
- 土木工程畢業(yè)設(shè)計(jì)(論文)-五層宿舍樓建筑結(jié)構(gòu)設(shè)計(jì)
- 青年卒中 幻燈
- 典型倒閘操作票
評(píng)論
0/150
提交評(píng)論