如何更改直方图条的颜色?

How do I change the color of histogram bars?

我确定在某个地方有这个问题的答案,但我在任何地方都找不到。

如何用另一组数据为直方图条形着色,使条形看起来像这样...

唯一的区别是条形的高度不同。

如果使用matplotlib模块,条形图有颜色参数。在此参数中,您可以更改颜色。这是我编辑的 matplotlib.org 中的一些代码示例,以便向您展示。

import matplotlib.pyplot as plt


labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 35, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
men_std = [2, 3, 4, 1, 2]
women_std = [3, 5, 2, 3, 3]
width = 0.35       # the width of the bars: can also be len(x) sequence

fig, ax = plt.subplots()

ax.bar(labels, men_means, width, yerr=men_std, label='Men', color = 'blue')
ax.bar(labels, women_means, width, yerr=women_std, bottom=men_means,
       label='Women', color = 'pink')

ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.legend()

plt.show()

此代码将生成图表 linked

Bar Chart Color Example

有很多不同的颜色可供选择。这是 matplotlib 可用颜色的 link。

https://matplotlib.org/stable/gallery/color/named_colors.html