【问题标题】:Matplotlib: how to create vertical lines between x-values on barplot?Matplotlib:如何在 barplot 上的 x 值之间创建垂直线?
【发布时间】:2021-01-19 18:48:23
【问题描述】:

我目前正在制作一个如下所示的条形图: 为了更清楚,我想添加一条垂直线,指出 x 值之间的分隔。我在这里画了一个例子:

为了绘制图表,我在相应的数据框上使用了 pandas 的 plot 函数:

produced_items.plot(kind='bar', legend=True,title="Produced Items - Optimal solution",xlabel="Months",ylabel='Amount',rot=1,figsize=(15,5),width=0.8)

我希望在 matplotlib 中找到一个能够产生所需行为的参数,但我没有找到任何可以提供帮助的东西。 我想到的另一个解决方案是在每个 x 值之间绘制一条垂直线,但我想知道是否有内置的方法来实现这一点?

提前致谢。

【问题讨论】:

  • 最简单的方法是操纵次要刻度。

标签: python pandas dataframe matplotlib


【解决方案1】:

让我们尝试修改次要刻度:

from matplotlib.ticker import MultipleLocator

ax = df.plot.bar()

# set the minor ticks
ax.xaxis.set_minor_locator(MultipleLocator(0.5))

# play with the parameters to get your desired output
ax.tick_params(which='minor', length=15, direction='in')

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 2021-05-10
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多