gpt4 book ai didi

python - Pandas groupby 函数中的 Secondary_y 范围

转载 作者:行者123 更新时间:2023-11-28 21:51:17 26 4
gpt4 key购买 nike

我想在一个简单的例子中改变次 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)

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)

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

最佳答案

Pandas plot 函数返回一个轴数组,您可以使用 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

关于python - Pandas groupby 函数中的 Secondary_y 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30582086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com