python - pandas bar plots x 轴的行为与直接校准 Axes.bar 不同
问题描述
我正在绘制一些时间序列数据,同时使用线图和条形图,并且在共享 X 轴时,我观察到 pandas 条形图上的古怪或烦人的行为。
- 在左侧,熊猫线图 + 直接调用
Axes.bar
。这是预期的结果。 - 右边是熊猫线图+熊猫条形图。线图的 x 轴自动变成浮点数,线在某处消失,可能是因为范围或比例的变化。此外,条形图的 x 轴显示每个日期(这可以用 纠正
ax[1, 1].xaxis.set_major_locator(mdates.MonthLocator())
,但结果不一样)。
在这种情况下,有没有办法让熊猫条形图表现得像原始 matplotlib 条形图?
代码:
# close100 and volume100 are time series with the same date index
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(12, 6))
close100.plot(ax=ax[0, 0])
ax[1, 0].bar(volume100.index, volume100.values) # Using Axes.bar
close100.plot(ax=ax[1, 0])
volume100.plot.bar(ax=ax[1, 1]) # Using Series.plot.bar
ax[1, 1].xaxis.set_major_locator(mdates.MonthLocator())
解决方案
推荐阅读
- mysql - 在所有排列中确保 FK 唯一性的最佳方法是什么?
- c++ - 任意数量的 Lambda/函数,并将捕获作为函数参数
- python - 使用 3 个嵌套的 for 循环是一种不好的做法吗?(或更多!)
- dart - 隐藏一行溢出的元素并添加一个显示更多按钮
- python - 当我执行 file.read(1) 时,有没有办法返回 1 个字节?
- python - 为什么我没有得到以下 python 代码的输出?
- typescript - Typescript 中的类型安全包装函数
- modelica - 在 OpenModelica 的结果变量浏览器中看到的 StateSet 变量的用途是什么
- graphql - Apollor 服务器 2 中的链接模式
- php - 代码点火器 | 有时在添加购物车项目时用户会话被破坏