可視化之如何用pandas繪制簡單的圖形
0. 簡介
python中常用的繪圖庫除了matplotlib、seaborn之外,數(shù)據(jù)處理庫pandas也能畫圖,本文就主要介紹如何使用pandas畫圖。
pandas主要是通過 pandas.DataFrame.plot()
來完成繪圖,因此我們先來了解此函數(shù)的各個參數(shù),然后通過實例進(jìn)行演示。
1. pandas.DataFrame.plot()參數(shù)
參數(shù):
DataFrame.plot(data, x=None, y=None, kind='line', ax=None, subplots=False, sharex=None, sharey=False, layout=None,figsize=None, use_index=True, title=None, grid=None, legend=True, style=None, logx=False, logy=False, loglog=False, xticks=None, yticks=None, xlim=None, ylim=None, rot=None, xerr=None,secondary_y=False, sort_columns=False, include_bool=False, **kwds)
解釋:
總體相關(guān)參數(shù)
Parameters: data: series或DataFrame格式 #可以通過pd.DataFrame.plot(data)調(diào)用,也可以通過data.plot(),此時就不需要傳入data參數(shù)。 x : label or position, default None # x軸的數(shù)據(jù),只有當(dāng)數(shù)據(jù)為DataFrame才傳入,可以是列名或列的位置,不傳入的話,默認(rèn)是DataFrame的索引 y : label or position, default None # y軸的數(shù)據(jù),只有當(dāng)數(shù)據(jù)為DataFrame才傳入,可以是列名或列的位置,不傳入的話,默認(rèn)是DataFrame的所有列 kind : str # 繪制圖形的種類 ‘line' : line plot (default)#折線圖 ‘bar' : vertical bar plot#條形圖 ‘barh' : horizontal bar plot#橫向條形圖 ‘hist' : histogram#直方圖 ‘box' : boxplot#箱線圖 ‘kde' : Kernel Density Estimation plot#Kernel 的密度估計圖,主要對柱狀圖添加Kernel 概率密度線 ‘density' : same as ‘kde' ‘a(chǎn)rea' : area plot#不了解此圖 ‘pie' : pie plot#餅圖 ‘scatter' : scatter plot#散點圖,只有數(shù)據(jù)為DataFrame格式才能傳入此參數(shù) ‘hexbin' : hexbin plot#不了解此圖,只有數(shù)據(jù)為DataFrame格式才能傳入此參數(shù)
子圖相關(guān)參數(shù)
ax : matplotlib axes object, default None # An axes(軸) of the current figure**子圖(axes, 也可以理解成坐標(biāo)軸) 要在其上進(jìn)行繪制的matplotlib subplot對象。如果沒有設(shè)置,則使用當(dāng)前matplotlib subplot**其中,變量和函數(shù)通過改變figure和axes中的元素(例如:title,label,點和線等等)一起描述figure和axes,也就是在畫布上繪圖。 subplots : boolean, default False # Make separate subplots for each column,就是說當(dāng)設(shè)置subplots=True時,會為每列制作單獨的子圖 sharex : boolean, default True if ax is None else False # 如果有子圖,子圖共x軸刻度、標(biāo)簽 sharey : boolean, default False#如果有子圖,子圖共y軸刻度,標(biāo)簽 layout : tuple (optional) # (rows, columns) for the layout of subplots,子圖的行列布局
圖片相關(guān)參數(shù)
figsize : a tuple (width, height) in inches # 圖片尺寸大小 use_index : boolean, default True # Use index as ticks for x axis,默認(rèn)用索引做x軸 title : str or list # 標(biāo)題。如果傳入了list而且subplots=True,為每個子圖設(shè)置標(biāo)題 grid : boolean, default None (matlab style default) #圖片是否有網(wǎng)格 legend : #子圖的圖例,添加一個subplot圖例(默認(rèn)為True) style : list or dict # The matplotlib line style per column,每列圖線的類型
x, y軸相關(guān)設(shè)置
logx : bool or ‘sym', default False # Use log scaling or symlog scaling on x axis,設(shè)置x軸刻度是否取對數(shù)或symlog(定義一個接近零的值范圍,在該范圍內(nèi)繪圖是線性的,以避免使繪圖趨于零附近的無窮大。) logy : bool or ‘sym', default False # Use log scaling or symlog scaling on y axis loglog : bool or ‘sym', default False#同時設(shè)置x,y軸刻度是否取對數(shù) xticks : sequence # Values to use for the xticks,設(shè)置x軸刻度值,序列形式(比如列表) yticks : sequence # Values to use for the yticks,設(shè)置y軸刻度,序列形式(比如列表) xlim : 2-tuple/list # 設(shè)置坐標(biāo)軸的范圍,列表或元組形式 ylim : 2-tuple/list # 設(shè)置坐標(biāo)軸的范圍,列表或元組形式 xlabel: label, optional # 用于x軸上的xlabel的名稱。默認(rèn)情況下,將索引名稱或用于x軸的列名用作xlabel ylabel: label, optional # 用于y軸上的xlabel的名稱。默認(rèn)情況下,沒有索引名稱,而是通過legend的形式展示名稱 rot : int, default None # Rotation for ticks (xticks for vertical, yticks for horizontal plots), 設(shè)置軸標(biāo)簽(軸刻度)的顯示旋轉(zhuǎn)度數(shù) fontsize : int, default None # Font size for xticks and yticks,設(shè)置軸刻度的字體大小
區(qū)域設(shè)置
colormap : str or matplotlib colormap object, default None # Colormap to select colors from. If string, load colormap with that name from matplotlib,設(shè)置圖的區(qū)域顏色 colorbar : boolean, optional # If True, plot colorbar (only relevant for ‘scatter' and ‘hexbin' plots) ??? position : float # Specify relative alignments for bar plot layout. From 0 (left/bottom-end) to 1 (right/top-end). Default is 0.5 (center) table : boolean, Series or DataFrame, default False # 如果為正,則選擇DataFrame類型的數(shù)據(jù)并且轉(zhuǎn)換匹配matplotlib的布局。 # If True, draw a table using the data in the DataFrame and the data will be transposed to meet matplotlib's default layout. If a Series or DataFrame is passed, use passed data to draw a table. yerr : DataFrame, Series, array-like, dict and str # ??? xerr : same types as yerr. # ??? stacked : boolean, default False in line and bar plots, and True in area plot. #If True, create stacked plot.如果設(shè)為真,則為堆疊圖,常見于條形圖 sort_columns : boolean, default False # 以字母表順序繪制各列,默認(rèn)使用前列順序 secondary_y : boolean or sequence, default False # Whether to plot on the secondary y-axis If a list/tuple, which columns to plot on secondary y-axis, 設(shè)置第二個y軸(右y軸) mark_right : boolean, default True # When using a secondary_y axis, automatically mark the column labels with “(right)” in the legend include_bool: bool, default is False # If True, boolean values can be plotted Returns:axes : matplotlib.AxesSubplot or np.array of them
2. 演示
2.1 各種圖
參考pandas 常見繪圖總結(jié)
test_dict = {'銷售量':[1000,2000,5000,2000,4000,3000],'收藏':[1500,2300,3500,2400,1900,3000]} data = pd.DataFrame(test_dict,index=['一月','二月','三月','四月','五月','六月']) data
銷售量 收藏
一月 1000 1500
二月 2000 2300
三月 5000 3500
四月 2000 2400
五月 4000 1900
六月 3000 3000
2.1.1 折線圖
普通折線圖以折線的上升或下降來表示統(tǒng)計數(shù)量的增減變化的統(tǒng)計圖,叫作折線統(tǒng)計圖。
折線統(tǒng)計圖不僅可以表示數(shù)量的多少,而且可以反映同一事物在不同時間里的發(fā)展變化的情況,雖然它不直接給出精確的數(shù)據(jù)(當(dāng)然你也可以加上去),但是能夠顯示數(shù)據(jù)的變化趨勢,反映事物的變化情況。
data.plot(kind='line')
2.1.2 條形圖
條形統(tǒng)計圖可以清楚地表明各種數(shù)量的多少。
普通條形圖:
data.plot(kind='bar')
堆積條形圖:
data.plot(kind='bar', stacked=True)
2.1.3 橫向條形圖
data.plot(kind='barh')
2.1.4 直方圖
直方圖是數(shù)值數(shù)據(jù)分布的精確圖形表示,這是一個連續(xù)變量(定量變量)的概率分布的估計
test_dict2 = {'泊松分布':np.random.poisson(50,100),'貝塔分布':np.random.beta(5,1,100)*40} data2 = pd.DataFrame(test_dict2) data2
泊松分布 貝塔分布 0 40 38.001607 1 50 36.588124 2 53 36.325921 3 49 37.138868 4 59 38.503662 ... ... ... 95 52 21.731441 96 48 37.274649 97 59 35.666431 98 60 30.777406 99 50 24.558308 100 rows × 2 columns
data2.plot(kind='hist', subplots=True, bins=20, sharex=False, layout=(1,2))
2.1.5 箱線圖
data.plot(kind='box')
2.1.6 核密度圖
data2.plot(kind='kde', subplots=True, grid=True)
2.1.7 餅圖
data.plot(kind='pie', subplots=True,figsize=(10, 8),autopct='%.2f%%',radius = 1.2,startangle = 250,legend=False,colormap='viridis')
此處有一些特殊的參數(shù):
figsize # 圖片的大小。 autopct # 顯示百分比。 radius # 圓的半徑。 startangle # 旋轉(zhuǎn)角度。 colormap # 顏色。
2.1.8 散點圖
data.plot(kind='scatter', x=0, y=1)
2.2 x軸ticks方向設(shè)置
設(shè)置rot=0
,則將ticks方向設(shè)置為水平
data.plot(kind='bar', rot=0)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
flask/django 動態(tài)查詢表結(jié)構(gòu)相同表名不同數(shù)據(jù)的Model實現(xiàn)方法
今天小編就為大家分享一篇flask/django 動態(tài)查詢表結(jié)構(gòu)相同表名不同數(shù)據(jù)的Model實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python標(biāo)準(zhǔn)庫之Sys模塊使用詳解
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫之Sys模塊使用詳解,本文講解了使用sys模塊獲得腳本的參數(shù)、處理模塊、使用sys模塊操作模塊搜索路徑、使用sys模塊查找內(nèi)建模塊、使用sys模塊查找已導(dǎo)入的模塊等使用案例,需要的朋友可以參考下2015-05-05使用Python進(jìn)行二進(jìn)制文件讀寫的簡單方法(推薦)
下面小編就為大家?guī)硪黄褂肞ython進(jìn)行二進(jìn)制文件讀寫的簡單方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09Python實現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法
這篇文章主要介紹了Python實現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法,涉及Python調(diào)用系統(tǒng)win32com組件實現(xiàn)文件格式轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-07-07詳解OpenCV中直方圖,掩膜和直方圖均衡化的實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了OpenCV中直方圖、掩膜、直方圖均衡化詳細(xì)介紹及代碼的實現(xiàn),文中的示例代碼講解詳細(xì),需要的可以參考一下2022-11-11