Use secondary_y
in pandas.DataFrame.plot https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html:
ax = df.plot('Date', 'A')
df.plot('Date', 'B', secondary_y=True, ax=ax)
更简洁地说:
ax = df.plot(x='Date', y=['A', 'B'], secondary_y='B', figsize=(8, 5))
# move the legend for each y axis
ax.legend(bbox_to_anchor=(1.05, 0.53), loc='center left', frameon=False)
ax.right_ax.legend(bbox_to_anchor=(1.05, 0.47), loc='center left', frameon=False)
(可选)使用ax.twinx()
ax = df.plot('Date','A')
ax1 = ax.twinx()
df.plot('Date','B',ax=ax1, color='r')
Output: