![用Pandas作圖_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/25/5283ef13-b025-4fff-a8e8-63d899a1adef/5283ef13-b025-4fff-a8e8-63d899a1adef1.gif)
![用Pandas作圖_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/25/5283ef13-b025-4fff-a8e8-63d899a1adef/5283ef13-b025-4fff-a8e8-63d899a1adef2.gif)
![用Pandas作圖_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/25/5283ef13-b025-4fff-a8e8-63d899a1adef/5283ef13-b025-4fff-a8e8-63d899a1adef3.gif)
![用Pandas作圖_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/25/5283ef13-b025-4fff-a8e8-63d899a1adef/5283ef13-b025-4fff-a8e8-63d899a1adef4.gif)
![用Pandas作圖_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/25/5283ef13-b025-4fff-a8e8-63d899a1adef/5283ef13-b025-4fff-a8e8-63d899a1adef5.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、 用Pandas作圖分類: python2014-07-11 12:20 32人閱讀 評論(0) 收藏 舉報目錄(?)+來自:/python/2014/02/23/Plotting_with_Pandas/#wat_e_12612920-6fe4-464e-a2b0-3b1f13c1a4f6_zss_關于Pandas的基本使用介紹,請查看另一篇博文:Python中的結構化數據分析利器-Pandas簡介推薦使用ipython的pylab模式,如果要在ipython notebook中嵌入圖片,則還需要指定
2、pylab=inline。ipython -pylab #ipython的pylab模式ipython notebook -pylab=inline #notebook的inline模式import pandas as pd基本畫圖命令Pandas通過整合matplotlib的相關功能實現了基于DataFrame的一些 作圖功能。下面的數據是每年美國男女出生數據:url = 'present = pd.read_table(url, sep=' ')present.shape(63, 3)present.columnsIndex(u'year', u
3、39;boys', u'girls', dtype='object')可以看到這個數據集共有63條記錄,共有三個字段:Year,boys,girls。為了簡化計算將year作為索引。present_year = present.set_index('year')plot是畫圖的最主要方法,Series和DataFrame都有plot方法??梢赃@樣看一下男生出生比例的趨勢圖:present_year'boys'.plot()plt.legend(loc='best')<matplotlib.legend
4、.Legend at 0x10b9c7610>這是Series上的plot方法,通過DataFrame的plot方法,你可以將男生和女生出生數量的趨勢圖畫在一起。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方法與在多個Series調用多次plot方法的效果是一致。present_year:10.plot(kind='bar')<matplotlib.axes.AxesSubplot at 0x10ab31390>plot默認生成是曲線圖,你可以通過kind參數生成其他的圖形,可選的值為: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>制作相對的累積柱狀圖,需要一點小技巧。首先需要計算每一行的匯總值,可以在DataFrame上直接調用sum方法,
7、參數為1,表示計算行的匯總。默認為0,表示計算列的匯總。present_year.sum(1):5year1940 23603991941 25134271942 28089961943 29368601944 2794800dtype: int64有了每一行的匯總值之后,再用每個元素除以對應行的匯總值就可以得出需要的數據。這里可以使用DataFrame的div函數,同樣要指定axis的值為0。present_year.div(present_year.sum(1),axis=0):10.plot(kind='barh', stacked=True)<matplotlib
8、.axes.AxesSubplot at 0x113223290>散點圖和相關plot也可以畫出散點圖。使用kind='scatter', x和y指定x軸和y軸使用的字段。present_year.plot(x='boys', y='girls', kind='scatter')<matplotlib.axes.AxesSubplot at 0x1141c9810>再來載入一下鳶尾花數據。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參數可以計算不同分組情況下,各個字段的箱圖。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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國全銅水槽落水頭市場調查研究報告
- 2025至2031年中國音頻電療機行業(yè)投資前景及策略咨詢研究報告
- 2025年窗配件項目可行性研究報告
- 2025至2031年中國電腦花織帶行業(yè)投資前景及策略咨詢研究報告
- 2025至2031年中國無紡紙行業(yè)投資前景及策略咨詢研究報告
- 2025年掛鎖項目可行性研究報告
- 2025年奧運熒光筆項目可行性研究報告
- 2025至2031年中國全自動圓角切斷機行業(yè)投資前景及策略咨詢研究報告
- 2025年便攜式酒精檢測儀項目可行性研究報告
- 2025年T/C染色線卡項目可行性研究報告
- 中考物理復習備考策略
- 博士后進站申請書博士后進站申請書八篇
- 小報:人工智能科技科學小報手抄報電子小報word小報
- GB/T 41509-2022綠色制造干式切削工藝性能評價規(guī)范
- 全面介紹現貨中遠期交易
- 公安系防暴安全03安檢
- 孫權勸學教案全國一等獎教學設計
- 企業(yè)生產現場6S管理知識培訓課件
- 五年級下冊數學課件 第10課時 練習課 蘇教版(共11張PPT)
- 電梯口包邊施工方案正式
- 三年級道德與法治下冊我是獨特的
評論
0/150
提交評論