简体   繁体   English

将列标签添加到条形图 (Python-matplotlib)

[英]Add columns labels to a bar plot (Python-matplotlib)

I have a list of scores:我有一个分数列表:

sincerity_scores=[[0.7386426068589986, 0.03521168088827334, 0.17283997838902623, 0.17978091813696798, 0.46179609926078796, 0.03232785417651009]

and I want to obtain a bar plot out of it.我想从中获得一个条形图。

I created the barplot with:我创建了条形图:

x=np.arange(6)

fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.bar(x, sincerity_scores)
ax.set_xticklabels(labels=["CocaCola", "Appletiser", "PepsiMax", "DrPepper", "MountainDew", "Welchs"])
plt.show()

but what I get is:但我得到的是:
在此处输入图片说明

while I would like the lables of the colums follow the sequence i indicated with as.set_xticklabels.虽然我希望列的标签遵循我用 as.set_xticklabels 指示的顺序。 How to do it?怎么做? Moreover, I would like to set a different colour.此外,我想设置不同的颜色。 Is it possible?是否可以?

change x to the list of your labels.x更改为您的标签列表。

sincerity_scores=[0.7386426068589986, 0.03521168088827334, 0.17283997838902623, 0.17978091813696798, 0.46179609926078796, 0.03232785417651009]
x=["CocaCola", "Appletiser", "PepsiMax", "DrPepper", "MountainDew", "Welchs"]

fig=plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.bar(x, sincerity_scores)
plt.show()

This results in this graph:这导致了这个图表:

在此处输入图片说明

color can be set using the color argument in ax.bar .可以使用ax.bar的 color 参数设置颜色。 like so像这样

ax.bar(x, sincerity_scores, color='k')

resulting in all bars being black.导致所有条形都是黑色的。 在此处输入图片说明

You can change 'k' for any matplotlib color or any hexcolor code you desire.您可以为任何 matplotlib 颜色或您想要的任何十六进制颜色代码更改'k' If you want each bar to have a different color feed a list of codes to color with the same length as you have bars.如果您希望每个条形具有不同的颜色,请提供与条形长度相同的颜色代码列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM