Python Pandas工具繪制數(shù)據(jù)圖使用教程
背景介紹
Pandas的DataFrame和Series在Matplotlib基礎(chǔ)上封裝了一個簡易的繪圖函數(shù),使得數(shù)據(jù)處理過程中方便可視化查看結(jié)果。
折線圖
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()

散點圖
數(shù)據(jù)通常是一些點的集合
常用來繪制各種相關(guān)性,適合研究不同變量間的關(guān)系
- x:x坐標(biāo)位置
- y:y坐標(biāo)位置
- s:散點的大小
- c:散點顏色
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()

箱線圖
基于最小值、上四分位、中位數(shù)、下四分位和最大值5個數(shù)值特征展示數(shù)據(jù)分布的標(biāo)準(zhǔn)方式,可以看出數(shù)據(jù)是否具有對稱性,適用于展示一組數(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 若每列繪制子圖就為True
layout:子圖布局
figsize:畫布大小
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 繪制圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實現(xiàn)byte轉(zhuǎn)integer
這篇文章主要介紹了Python實現(xiàn)byte轉(zhuǎn)integer操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Python?Pandas聚合函數(shù)的應(yīng)用示例
Pandas是當(dāng)前Python數(shù)據(jù)分析中最為重要的工具,其提供了功能強大且靈活多樣的API,可以滿足使用者在數(shù)據(jù)分析和處理中的多種選擇和實現(xiàn)方式,下面這篇文章主要給大家介紹了關(guān)于Python?Pandas聚合函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-07-07
Python使用scrapy采集數(shù)據(jù)時為每個請求隨機分配user-agent的方法
這篇文章主要介紹了Python使用scrapy采集數(shù)據(jù)時為每個請求隨機分配user-agent的方法,涉及Python使用scrapy采集數(shù)據(jù)的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
python中的不可變數(shù)據(jù)類型與可變數(shù)據(jù)類型詳解
探尋python的數(shù)據(jù)類型是否可變,也可以更好的理解python對內(nèi)存的使用情況,下面這篇文章主要給大家介紹了關(guān)于python中不可變數(shù)據(jù)類型與可變數(shù)據(jù)類型的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09

