【发布时间】:2015-11-24 12:20:53
【问题描述】:
我使用 Pandas 数据框创建了一个绘图,其中我在主要 Y 轴上有一个条形图,在次要 Y 轴上有一个折线图。X 轴很常见,因此使用了 twinx 方法。
这是我使用的代码
ax = df.plot(x=['TIME'],y =['Column A'],kind='bar',use_index=True,color='g',legend=True)
ax.set_ylabel("Label A")
ax2 = ax.twinx()
ax2.plot(df[['Column B']].values,linestyle='-', marker='o', linewidth=2.0,color ='b')
ax2.set_ylabel("Label B")
这将创建所需的图表,但仅显示主要图例。我尝试将 ax2.plot 线更改为
ax2.plot(df[['Column B']].values,linestyle='-', marker='o', linewidth=2.0,color ='b',legend=True)
但这给了我一个错误,因为“TypeError: There is no line property "legend"” 你们可以看看如何显示属于这两个属性的组合图例。
非常感谢。 莫晨
【问题讨论】:
标签: python pandas matplotlib