secondary_y=True changes x axis in pandas

寵の児 提交于 2019-11-29 12:46:35
import matplotlib.pyplot as plt
import pandas as pd
from numpy.random import random

df = pd.DataFrame(random((15,2)),columns=['a','b'])
df.a = df.a*100

fig, ax1 = plt.subplots(1,1)
df.a.plot(ax=ax1, color='blue', label='a')
ax2 = ax1.twinx()
df.b.plot(ax=ax2, color='green', label='b')
ax1.set_ylabel('a')
ax2.set_ylabel('b')
ax1.legend(loc=3)
ax2.legend(loc=0)
plt.show()

I had the same issue, always getting a strange plot when I wanted a secondary_y.

I don't know why no-one mentioned this method in this post, but here's how I got it to work, using the same example as cphlewis:

import matplotlib.pyplot as plt
import pandas as pd
from numpy.random import random

df = pd.DataFrame(random((15,2)),columns=['a','b'])
ax = df.plot(secondary_y=['b'])
plt.show()

Here's what it'll look like

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!