使用secondary_y轴绘制分组数据 [英] Plot groupby data using secondary_y axis

查看:94
本文介绍了使用secondary_y轴绘制分组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制12张图(每月一张图),其中y轴的左侧为'A''B'列,右侧为'C'列.

I would like to plot 12 graphs (one graph per month) including columns 'A' and 'B' on the left y axis and column 'C' on the right.

下面的代码在左侧绘制了所有内容.

Code below plots everything on the left side.

import pandas as pd
index=pd.date_range('2011-1-1 00:00:00', '2011-12-31 23:50:00', freq='1h')
df=pd.DataFrame(np.random.rand(len(index),3),columns=['A','B','C'],index=index)

df2 = df.groupby(lambda x: x.month)
for key, group in df2:
    group.plot()

如何分隔列并使用类似这样的内容:group.plot({'A','B':style='g'},{'C':secondary_y=True})?

How to separate columns and use something like this:group.plot({'A','B':style='g'},{'C':secondary_y=True}) ?

推荐答案

您可以捕获熊猫plot()命令返回的轴,并再次使用它在右轴上绘制C.

You can capture the axes which the Pandas plot() command returns and use it again to plot C specifically on the right axis.

index=pd.date_range('2011-1-1 00:00:00', '2011-12-31 23:50:00', freq='1h')
df=pd.DataFrame(np.random.randn(len(index),3).cumsum(axis=0),columns=['A','B','C'],index=index)

df2 = df.groupby(lambda x: x.month)
for key, group in df2:
    ax = group[['A', 'B']].plot()
    group[['C']].plot(secondary_y=True, ax=ax)

要在单个图例中获取所有行,请参见: 图例在与熊猫绘图时仅显示一个标签

To get all lines in a single legend see: Legend only shows one label when plotting with pandas

这篇关于使用secondary_y轴绘制分组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆