我可能做错了什么,但我正在努力实现以下目标:
# plot bars and lines in the same figure, sharing both x and y axes.
df = some DataFrame with multiple columns
_, ax = plt.subplots()
df[col1].plot(kind='bar', ax=ax)
df[col2].plot(ax=ax, marker='o', ls='-')
ax.legend(loc='best')
我希望看到一张图表both一些条形和一条线。然而,我最终得到的只是一行df[col2]
, 酒吧从df[col1]
只是不在图表上。无论之前是什么df[col2]
似乎已被覆盖。
我用以下方法解决了这个问题:
df[col1].plot(kind='bar', ax=ax, label=bar_labels)
ax.plot(df[col2], marker='o', ls='-', label=line_labels)
ax.legend(loc='best')
但是,这并不完美,因为我必须使用label
标签,否则图例将不包含以下项目df[col2]
...
有人有更优雅的解决方案来使条形图和线条都显示出来吗?
** 编辑 **
感谢@DizietAsahi - 发现这是 DatetimeIndex 作为 x 值的问题。在 Pandas 提交了以下内容:
https://github.com/pydata/pandas/issues/10761#issuecomment-128671523