简体   繁体   English

使用secondary_y轴绘制分组数据

[英]Plot groupby data using secondary_y axis

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. 我想绘制12张图表(每月一张图表),其中y轴的左侧为'A''B'列,右侧为'C'列。

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()

How to separate columns and use something like this: group.plot({'A','B':style='g'},{'C':secondary_y=True}) ? 如何分隔列并使用类似这样的东西: group.plot({'A','B':style='g'},{'C':secondary_y=True})吗?

You can capture the axes which the Pandas plot() command returns and use it again to plot C specifically on the right axis. 您可以捕获Pandas plot()命令返回的轴,并再次使用它在右轴上专门绘制C

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 要在单个图例中获取所有行,请参见: 图例在与熊猫一起绘制时仅显示一个标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM