Pandas groupby函数中的secondary_y范围

Mic*_*hal 2 python matplotlib pandas

我想在一个简单的例子中更改辅助Y轴的范围:

MWE:

index=pd.date_range('2014-1-1 00:00:00', '2014-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)

df_month = df.groupby(lambda x: x.month)
df_month.plot(secondary_y=['C'],mark_right=False)
Run Code Online (Sandbox Code Playgroud)

groupby.plot功能中我只能为左轴设置ylim.如何更改右轴范围

我也试图循环键入groupby:

for key, group in df_month:
    ax = group[['A','B']].plot()
    fig= group[['C']].plot(secondary_y=True, ax=ax, mark_right=False)
Run Code Online (Sandbox Code Playgroud)

使用ax2 = ax1.twinx()变体,但它没有成功.

Mol*_*lly 5

Pandas绘图函数返回一个轴数组,您可以使用ax.right_ax从每个轴获取右轴.

axs = df_month.plot(secondary_y=['C'], mark_right=False)

for ax in axs:
    ax.right_ax.set_ylim((0,50)) # Set the y limits to 0 to 50
Run Code Online (Sandbox Code Playgroud)