我想這可能工作:
import matplotlib.pyplot as plt
import numpy as np
from pandas import DataFrame
df = DataFrame(np.random.randn(5, 3), columns=['A', 'B', 'C'])
fig, ax = plt.subplots()
ax3 = ax.twinx()
rspine = ax3.spines['right']
rspine.set_position(('axes', 1.15))
ax3.set_frame_on(True)
ax3.patch.set_visible(False)
fig.subplots_adjust(right=0.7)
df.A.plot(ax=ax, style='b-')
# same ax as above since it's automatically added on the right
df.B.plot(ax=ax, style='r-', secondary_y=True)
df.C.plot(ax=ax3, style='g-')
# add legend --> take advantage of pandas providing us access
# to the line associated with the right part of the axis
ax3.legend([ax.get_lines()[0], ax.right_ax.get_lines()[0], ax3.get_lines()[0]],\
['A','B','C'], bbox_to_anchor=(1.5, 0.5))
輸出:

這是偉大的。但是我們需要ax2嗎? – goofd 2014-02-28 00:27:06
使用這種方法我遇到了重疊的圖例問題。這對我有幫助:'ax3.legend([ax.get_lines()[0],ax2.get_lines()[0],ax3.get_lines()[0]],[label1','label2','label3'] ,bbox_to_anchor =(1.15,0.5))' – tworec 2017-04-12 11:05:06
嗨!我想知道如何爲每個軸添加'ylabel'?謝謝! – 2018-01-27 08:05:42