pandas 熊猫辅助轴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46063379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:23:59  来源:igfitidea点击:

Pandas Secondary Axis

pandas

提问by J.D. Marlin

I have the following data frame

我有以下数据框

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

When I run this:

当我运行这个时:

df.plot(x='Date', y='A')
df.B.plot(secondary_y=True)

I get the following error:

我收到以下错误:

> 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

And my graph looks like this underneath the error (blue and red should overlap): Blue and red should overlap

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

回答by Scott Boston

IIUC:

IUC:

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

Output:

输出:

enter image description here

在此处输入图片说明

Or you can use secondary_yin Pandas plot:

或者您可以secondary_y在 Pandas 图中使用:

ax = df.plot('Date','A')
df.plot('Date','B',secondary_y=True, ax=ax)

Output:

输出:

enter image description here

在此处输入图片说明