pandas.DataFrame.plot( )参数详解

参考:链接

介绍

使用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)

注意:每种绘图类型都有相对应的方法

主要参数详细解释

官网:http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html
Eg.hexbin蜂巢图:http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.hexbin.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轴刻度值,序列形式(比如列表)

对图无影响,只是改变坐标轴,如何改坐标轴的刻度Eg.转化为文字?

  • yticks : sequence#设置y轴刻度,序列形式(比如列表)
  • xlim : float/2-tuple/list#设置坐标轴的范围。数值(最小值),列表或元组(区间范围)
  • ylim : float/2-tuple/list
  • rot : int, default None#设置轴标签(轴刻度)的显示旋转度数

X轴刻度旋转,Y轴呢?

  • 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

其他说明

其他参数

  • color:颜色
  • s:散点图大小
  • 散点图中参数c,s组合使用目的?

设置X、Y轴名称
ax.set_ylabel(‘yyy’)
ax.set_xlabel(‘xxx’)

plt.legend(loc=‘best’)

  • loc:图列位置

其他画图步骤

1)首先定义画图的画布:fig = plt.figure( )
2)然后定义子图ax ,使用 ax= fig.add_subplot( 行,列,位置标)
3)用 ax.plot( )函数或者 df.plot(ax = ax)
4)结尾加plt.show()

举例

官方例子:http://pandas.pydata.org/pandas-docs/version/0.18.1/visualization.html
主次Y轴及图例位置secondary_y

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
#     ts = ts.cumsum()
    df =pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD')) 
    df = df.cumsum()
    print(df)
    # 图1:其中A图用左Y轴标注,B图用右Y轴标注,二者共用一个X轴
    df.A.plot()#对A列作图,同理可对行做图
    df.B.plot(secondary_y=True, style='g')#设置第二个y轴(右y轴)
    # 图2
    ax = df.plot(secondary_y=['A', 'B'])# 定义column A B使用右Y轴。ax(axes)可以理解为子图,也可以理解成对黑板进行切分,每一个板块就是一个axes
#     ax = df.plot(secondary_y=['A', 'B'], mark_right=False)#上一行默认图列会显示(right), mark_right=False即关闭显示
    ax.set_ylabel('CD scale')
    ax.right_ax.set_ylabel('AB scale')
    ax.legend(loc='upper left') #设置图例的位置
    ax.right_ax.legend(loc='upper right')
#     ax.legend(loc='1') 
#     plt.legend(loc='2')zhem
    # 展示
    plt.show()

在这里插入图片描述
箱型图颜色及横向分布vert

df = DataFrame([[1,2,3],[2,3,4],[3,4,2],[4,6,1],[5,8,8],],columns=['A','B','C'],index = np.arange(1,6))
colorDic = dict(boxes='DarkGreen', whiskers='DarkOrange', medians='DarkBlue', caps='Gray')
df.plot(kind='box',color=colorDic, sym='r+' ,vert=False)
plt.show()

在这里插入图片描述
X轴为时间时的良好展示

    # 参数x_compat=True实现:
    ts = Series(np.random.randn(1000), index=pd.date_range('1/1/2000',periods=1000)) 
    df = DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD')) 
    df = df.cumsum() 
#     df.A.plot()
    df.A.plot(x_compat=True)
    plt.show()

在这里插入图片描述

多组图形

方法一

    df = pd.DataFrame(np.random.randn(1000, 4), index=pd.date_range('1/1/2000',periods=1000), columns=list('ABCD'))
    df = df.cumsum()
    with pd.plotting.plot_params.use('x_compat', True): #方法一
        df.A.plot(color='r')
        df.B.plot(color='g')
        df.C.plot(color='b')
    plt.show()

在这里插入图片描述
方法二

    df = DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
    ax = df.plot.scatter(x='a', y='b', color='DarkBlue', label='Group 1');
    df.plot.scatter(x='c', y='d', color='Red', label='Group 2', ax=ax,s=100); #方法二ax,s控制点的大小
plt.show()

在这里插入图片描述

自定义图片布局ax

方法一

    fig, axes = plt.subplots(4, 4, figsize=(6, 6)) 
    plt.subplots_adjust(wspace=0.5, hspace=0.5) 
    target1 = [axes[0][0], axes[1][1], axes[2][2], axes[3][3]] 
    target2 = [axes[3][0], axes[2][1], axes[1][2], axes[0][3]] 
    df.plot(subplots=True, ax=target1, legend=False, sharex=False, sharey=False)
(-df).plot(subplots=True, ax=target2, legend=False, sharex=False, sharey=False)

方法二

    fig, axes = plt.subplots(nrows=2, ncols=2) 
    df['A'].plot(ax=axes[0,0]); axes[0,0].set_title('A') 
    df['B'].plot(ax=axes[0,1]); axes[0,1].set_title('B') 
    df['C'].plot(ax=axes[1,0]); axes[1,0].set_title('C') 
    df['D'].plot(ax=axes[1,1]); axes[1,1].set_title('D')

误差线xerr/yerr

    # Generate the data
    ix3 = pd.MultiIndex.from_arrays([['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b'], ['foo', 'foo', 'bar', 'bar', 'foo', 'foo', 'bar', 'bar']], names=['letter', 'word'])
    df3 = pd.DataFrame({'data1': [3, 2, 4, 3, 2, 4, 3, 2], 'data2': [6, 5, 7, 5, 4, 5, 6, 5]}, index=ix3) 
    # Group by index labels and take the means and standard deviations for each group 
    gp3 = df3.groupby(level=('letter', 'word'))
    means = gp3.mean() 
    errors = gp3.std() 
    means.plot.bar(yerr=errors,rot=45)
    plt.show()

在这里插入图片描述
色彩colormap

    df = pd.DataFrame(np.random.randn(1000, 10), index=range(1,1001)) 
    df = df.cumsum() 
    df.plot(colormap='cubehelix')#色彩暗淡,类型多
#     df.plot(colormap='gist_rainbow')#色彩艳丽,类型多(建议使用)
#     df.plot(colormap='prism')#色彩艳丽,六个颜色循环
    plt.show()
    #其他绘图类型Greens、……

colormap这个类当中的函数用法见http://matplotlib.org/api/cm_api.html
colormap所能够用到的颜色见http://scipy.github.io/old-wiki/pages/Cookbook/Matplotlib/Show_colormaps
填充线fill_between

    price = pd.Series(np.random.randn(150).cumsum(), index=pd.date_range('2000-1-1', periods=150, freq='B')) 
    ma = price.rolling(20).mean() 
    mstd = price.rolling(20).std() 
    plt.plot(price.index, price, 'k') 
    plt.plot(ma.index, ma, 'b') 
    plt.fill_between(mstd.index, ma-2*mstd, ma+2*mstd, color='b', alpha=0.2)
    plt.show()

在这里插入图片描述

参考网址

1、学习pandas下的dataframe画图参数

2、python:利用pandas进行绘图(总结)绘图格式

3、pandas.DataFrame.plot官网

4、官网举例

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值