【发布时间】:2015-07-22 05:49:13
【问题描述】:
我用相同的 x 轴从同一个 DataFrame 中绘制了两个 Pandas 系列,一切正常。但是,当我尝试手动创建图例时,它会出现,但只有标题而不是实际内容。我已经尝试过其他解决方案,但没有任何运气。这是我的代码:
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
width = .3
df.tally.plot(kind='bar', color='red', ax=ax1, width=width, position=1, grid=False)
df.costs.plot(kind='bar', color='blue', ax=ax2, width=width, position=0, grid=True)
ax1.set_ylabel('Tally')
ax2.set_ylabel('Total Cost')
handles1, labels1 = ax1.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()
plt.legend([handles1, handles2], [labels1, labels2], loc='upper left', title='Legend')
plt.show()
plt.clf()
【问题讨论】:
-
尝试传递
label='label'kwarg? -
为什么需要这样做?为什么不
df[['tally', 'costs']].plot(...?
标签: python pandas matplotlib plot