使用DataFrame的plot方法繪制圖像會按照數據的每一列繪制一條曲線,默認按照列columns的名稱在適當的位置展示圖例,比matplotlib繪制節省時間,且DataFrame格式的數據更規范,方便向量化及計算。
DataFrame.plot( )函數:
DataFrame.plot(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, fontsize=None, colormap=None,
position=0.5, table=False, yerr=None, xerr=None, stacked=True/False,
sort_columns=False, secondary_y=False, mark_right=True, **kwds)
注意:每種繪圖類型都有相對應的方法; Eg. df.plot(kind='line')與df.plot.line()等價
主要參數詳細解釋
官網:http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html
源碼:https://github.com/pandas-dev/pandas/blob/v0.24.2/pandas/plotting/core.py#L2912-L3605
- x : label or position, default None#指數據列的標簽或位置參數
- y : label, position or list of label, positions, default None
- kind : str#繪圖類型
‘line’ : line plot (default)#折線圖
‘bar’ : vertical bar plot#條形圖。stacked為True時為堆疊的柱狀圖
‘barh’ : horizontal bar plot#橫向條形圖
‘hist’ : histogram#直方圖(數值頻率分布)
‘box’ : boxplot#箱型圖
‘kde’ : Kernel Density Estimation plot#密度圖,主要對柱狀圖添加Kernel 概率密度線
‘density’ : same as ‘kde’
‘area’ : area plot#與x軸所圍區域圖(面積圖)。Stacked=True時,每列必須全部為正或負值,stacked=False時,對數據沒有要求
‘pie’ : pie plot#餅圖。數值必須為正值,需指定Y軸或者subplots=True
‘scatter’ : scatter plot#散點圖。需指定X軸Y軸
‘hexbin’ : hexbin plot#蜂巢圖。需指定X軸Y軸
- ax : matplotlib axes object, default None#**子圖(axes, 也可以理解成坐標軸) 要在其上進行繪制的matplotlib subplot對象。如果沒有設置,則使用當前matplotlib subplot**其中,變量和函數通過改變figure和axes中的元素(例如:title,label,點和線等等)一起描述figure和axes,也就是在畫布上繪圖。
- subplots : boolean, default False#是否對列分別作子圖
- sharex : boolean, default True if ax is None else False#如果ax為None,則默認為True,否則為False In case subplots=True, share x axis and set some x axis labels to invisible; defaults to True if ax is None otherwise False if an ax is passed in; Be aware, that passing in both an ax and sharex=True will alter all x axis labels for all axis in a figure!
- sharey : boolean, default False#如果有子圖,子圖共y軸刻度,標簽 In case subplots=True, share y axis and set some y axis labels to invisible
- layout : tuple (rows, columns) for the layout of subplots#子圖的行列布局
- figsize : a tuple (width, height) in inches#圖片尺寸大小
- use_index : boolean, default True#默認用索引做x軸
- title : string#圖片的標題用字符串 Title to use for the plot
- grid : boolean, default None#圖片是否有網格
- legend : False/True/’reverse’#子圖的圖例 (默認為True)
- style : list or dict#對每列折線圖設置線的類型
- logx : boolean, default False#設置x軸刻度是否取對數
- logy : boolean, default False
- loglog : boolean, default False#同時設置x,y軸刻度是否取對數
- xticks : sequence#設置x軸刻度值,序列形式(比如列表)
- yticks : sequence#設置y軸刻度,序列形式(比如列表)
- xlim : float/2-tuple/list#設置坐標軸的范圍。數值(最小值),列表或元組(區間范圍)
- ylim : float/2-tuple/list
- rot : int, default None#設置軸標簽(軸刻度)的顯示旋轉度數
- fontsize : int, default None#設置軸刻度的字體大小
- colormap : str or matplotlib colormap object, default None#設置圖的區域顏色
- colorbar : boolean, optional #柱子顏色 If True, plot colorbar (only relevant for ‘scatter’ and ‘hexbin’ plots)
- position : float #條形圖的對齊方式,取值范圍[0,1],即左下端到右上端默認0.5(中間對齊)
- layout : tuple (optional) #布局。layout=(2, 3)兩行三列,layout=(2, -1)兩行自適應列數 Eg. df.plot(subplots=True, layout=(2, -1), sharex=False)
- table : boolean, Series or DataFrame, default False #圖下添加表。如果為True,則使用DataFrame中的數據繪制表格,並且數據將被轉置以滿足matplotlib的默認布局。。
- yerr : DataFrame, Series, array-like, dict and str
- See Plotting with Error Bars for detail.
- xerr : same types as yerr.
- stacked : boolean, default False in line and bar plots, and True in area plot. If True, create stacked plot. #前面有介紹
- sort_columns : boolean, default False #對列名稱進行排序以確定繪圖順序
- secondary_y : boolean or sequence, default False #設置第二個y軸(右輔助y軸) Whether to plot on the secondary y-axis If a list/tuple, which columns to plot on secondary y-axis
- mark_right : boolean, default True When using a secondary_y axis, automatically mark the column labels with “(right)” in the legend
2. 其他說明
2.1 其他參數
color:顏色
s:散點圖大小
2.2 設置X、Y軸名稱
ax.set_ylabel('yyy')
ax.set_xlabel('xxx')
2.3 plt.legend(loc='best')
loc:圖列位置
3. 其他畫圖步驟
1)首先定義畫圖的畫布:fig = plt.figure( )
2)然后定義子圖ax ,使用 ax= fig.add_subplot( 行,列,位置標)
3)用 ax.plot( )函數或者 df.plot(ax = ax)
4)結尾加plt.show()
注意:在jupternotebook 需要用%定義:%matplotlib notebook;如果是在腳本編譯器上則不用,但是需要一次性按流程把代碼寫完
4. 參考網址
4)官網舉例