如何更改条形图中条形的颜色?
species = ("Adelie", "Chinstrap", "Gentoo")
penguin_means = {
'Bill Depth': (18.35, 18.43, 14.98),
'Bill Length': (38.79, 48.83, 47.50),
'Flipper Length': (189.95, 195.82, 217.19),
}
x = np.arange(len(species)) # the label locations
width = 0.25 # the width of the bars
multiplier = 0
fig, ax = plt.subplots(layout='constrained')
for attribute, measurement in penguin_means.items():
offset = width * multiplier
rects = ax.bar(x + offset, measurement, width, label=attribute)
ax.bar_label(rects, padding=3)
multiplier += 1
ax.set_ylabel('Length (mm)')
ax.set_title('Penguin attributes by species')
ax.set_xticks(x + width, species)
ax.legend(loc='upper left', ncols=3)
ax.set_ylim(0, 250)
plt.show()`
字符串
预期:
我想将条形图的颜色更改为:color = ['lightsalmon', 'lightseagreen', 'darkseagreen', 'mediumorchid']
1条答案
按热度按时间uqdfh47h1#
zip
将合并colors
与penguin_means.items()
合并,并在它们之间切换,这允许在ax.bar
中使用color
参数。字符串
pandas
创建pands.DataFrame
和pandas.DataFrame.plot
更容易地创建分组条形图。型
的数据
df
第一代型