【问题标题】:How to make bars in bar charts fill all the gaps如何使条形图中的条形填补所有空白
【发布时间】:2020-10-01 16:36:36
【问题描述】:

我对 matplotlib.pyplot.bar 有一个问题,其中条形只能具有恒定宽度,但我想制作一个图,其中条形位于两点之间。正如我们在图中看到的,点有不同的距离,我想用条形填充所有的间隙。抱歉,如果之前有人问过,我找不到。

所以基本上我想指出我在ii+1 之间的距离的宽度,如果这有意义的话。

【问题讨论】:

标签: python matplotlib bar-chart


【解决方案1】:

以下示例创建一个具有 7 个高度值和 8 个边界的条形图。连续边界之间的差异用作条形宽度。条形默认与其中心对齐,但在使用不均匀宽度时,需要align='edge'

from matplotlib import pyplot as plt
import numpy as np

bounds = np.array([21000, 31000, 41000, 53000, 62000, 73000, 81000, 90000])
heights = np.random.randint(50000, 120000, len(bounds) - 1)

plt.bar(bounds[:-1], heights, width=np.diff(bounds), align='edge', color='crimson', ec='navy')
# plt.xticks(bounds) # to have the boundaries as x ticks
plt.show()

【讨论】:

    猜你喜欢
    • 2020-11-14
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多