gpt4 book ai didi

Pandas 辅助轴

转载 作者:行者123 更新时间:2023-12-01 09:47:17 26 4
gpt4 key购买 nike

我有以下数据框

    Date        A           B
0 2017-05-31 17453139 5.865738
1 2017-06-30 17425164 5.272728
2 2017-07-31 17480789 4.843094

当我运行这个:
df.plot(x='Date', y='A')
df.B.plot(secondary_y=True)

我收到以下错误:
> appdata\local\programs\python\python36\lib\site-packages\pandas\plotting\_timeseries.py
> in format_dateaxis(subplot, freq, index)
> 335 TimeSeries_TimedeltaFormatter())
> 336 else:
> --> 337 raise TypeError('index type not supported')
> 338
> 339 pylab.draw_if_interactive()
>
> TypeError: index type not supported

我的图表在错误下方看起来像这样(蓝色和红色应该重叠):
Blue and red should overlap

最佳答案

IUC:

ax = df.plot('Date','A')
ax1 = ax.twinx()
df.plot('Date','B',ax=ax1, color='r')

输出:

enter image description here

或者您可以使用 secondary_y在 Pandas 情节中:
ax = df.plot('Date','A')
df.plot('Date','B',secondary_y=True, ax=ax)

输出:

enter image description here

关于 Pandas 辅助轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46063379/

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