Python Pandas工具繪制數(shù)據(jù)圖使用教程
背景介紹
Pandas的DataFrame和Series在Matplotlib基礎(chǔ)上封裝了一個(gè)簡(jiǎn)易的繪圖函數(shù),使得數(shù)據(jù)處理過(guò)程中方便可視化查看結(jié)果。
折線(xiàn)圖
import pandas as pd import numpy as np import matplotlib.pyplot as plt data=np.random.randn(5,2)*10 df=pd.DataFrame(np.abs(data),index=[1,2,3,4,5],columns=[1,2]) df.plot() plt.show()

條形圖
import pandas as pd import numpy as np import matplotlib.pyplot as plt data=np.random.randn(5,2)*10 df=pd.DataFrame(np.abs(data),index=[1,2,3,4,5],columns=[1,2]) df.plot(kind='bar') plt.show()

水平條形圖
import pandas as pd import numpy as np import matplotlib.pyplot as plt data=np.random.randn(5,2)*10 df=pd.DataFrame(np.abs(data),index=[1,2,3,4,5],columns=[1,2]) df.plot(kind='barh') plt.show()

堆積圖
import pandas as pd import numpy as np import matplotlib.pyplot as plt data=np.random.randn(5,2)*10 df=pd.DataFrame(np.abs(data),index=[1,2,3,4,5],columns=[1,2]) df.plot(kind='bar',stacked=True) plt.show()

import pandas as pd import numpy as np import matplotlib.pyplot as plt data=np.random.randn(5,2)*10 df=pd.DataFrame(np.abs(data),index=[1,2,3,4,5],columns=[1,2]) df.plot(kind='barh',stacked=True) plt.show()

散點(diǎn)圖
數(shù)據(jù)通常是一些點(diǎn)的集合
常用來(lái)繪制各種相關(guān)性,適合研究不同變量間的關(guān)系
- x:x坐標(biāo)位置
- y:y坐標(biāo)位置
- s:散點(diǎn)的大小
- c:散點(diǎn)顏色
import pandas as pd import numpy as np import matplotlib.pyplot as plt data=np.random.randn(5,2)*10 df=pd.DataFrame(np.abs(data),index=[1,2,3,4,5],columns=['A','B']) df.plot(kind='scatter',x='A',y='B',s=df.A*100,c='red') plt.show()

餅圖
import pandas as pd import numpy as np import matplotlib.pyplot as plt df=pd.Series(3*np.random.rand(4),index=['a','b','c','d']) df.plot.pie(figsize=(6,6)) plt.show()

蜂巢圖
體現(xiàn)數(shù)據(jù)出現(xiàn)的次數(shù)
import pandas as pd import numpy as np import matplotlib.pyplot as plt df=pd.DataFrame(np.random.randn(1000,2),columns=['a','b']) df.plot.hexbin(x='a',y='b',sharex=False,gridsize=30) plt.show()

箱線(xiàn)圖
基于最小值、上四分位、中位數(shù)、下四分位和最大值5個(gè)數(shù)值特征展示數(shù)據(jù)分布的標(biāo)準(zhǔn)方式,可以看出數(shù)據(jù)是否具有對(duì)稱(chēng)性,適用于展示一組數(shù)據(jù)的分布情況
import pandas as pd import numpy as np import matplotlib.pyplot as plt df=pd.DataFrame(np.random.randn(1000,2),columns=['a','b']) df.plot(y=df.columns,kind='box',vert=False) plt.show()

繪制子圖
subplots:默認(rèn)False 若每列繪制子圖就為T(mén)rue
layout:子圖布局
figsize:畫(huà)布大小
import pandas as pd import numpy as np import matplotlib.pyplot as plt df=pd.DataFrame(np.random.randn(5,2),columns=['a','b']) df.plot(subplots=True,layout=(2,3),figsize=(10,10),kind='bar') plt.show()

以上就是Python Pandas工具繪制數(shù)據(jù)圖使用教程的詳細(xì)內(nèi)容,更多關(guān)于Python Pandas 繪制圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python實(shí)現(xiàn)web方式logview的方法
這篇文章主要介紹了python實(shí)現(xiàn)web方式logview的方法,涉及Python基于web模塊操作Linux命令的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
Python實(shí)現(xiàn)byte轉(zhuǎn)integer
這篇文章主要介紹了Python實(shí)現(xiàn)byte轉(zhuǎn)integer操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Python?Pandas聚合函數(shù)的應(yīng)用示例
Pandas是當(dāng)前Python數(shù)據(jù)分析中最為重要的工具,其提供了功能強(qiáng)大且靈活多樣的API,可以滿(mǎn)足使用者在數(shù)據(jù)分析和處理中的多種選擇和實(shí)現(xiàn)方式,下面這篇文章主要給大家介紹了關(guān)于Python?Pandas聚合函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-07-07
Python使用scrapy采集數(shù)據(jù)時(shí)為每個(gè)請(qǐng)求隨機(jī)分配user-agent的方法
這篇文章主要介紹了Python使用scrapy采集數(shù)據(jù)時(shí)為每個(gè)請(qǐng)求隨機(jī)分配user-agent的方法,涉及Python使用scrapy采集數(shù)據(jù)的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
Pytorch實(shí)現(xiàn)邏輯回歸分類(lèi)
這篇文章主要為大家詳細(xì)介紹了Pytorch實(shí)現(xiàn)邏輯回歸分類(lèi),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
python中的不可變數(shù)據(jù)類(lèi)型與可變數(shù)據(jù)類(lèi)型詳解
探尋python的數(shù)據(jù)類(lèi)型是否可變,也可以更好的理解python對(duì)內(nèi)存的使用情況,下面這篇文章主要給大家介紹了關(guān)于python中不可變數(shù)據(jù)類(lèi)型與可變數(shù)據(jù)類(lèi)型的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09

