如何为Matplotlib条形图中的特定列设置不同的颜色?
我创建了一个10个足球俱乐部的酒吧图,其中最高和10的FIFA收视率。 我以这种方式绘制了同一数字:
grouped_top=df.groupby("club_name")["overall"].mean().reset_index().sort_values("overall",ascending=False).head(10)
grouped_bottom=df.groupby("club_name")["overall"].mean().reset_index().sort_values("overall",ascending=False).tail(10)
unioned=pd.concat([grouped_top,grouped_bottom])
unioned.plot(kind="bar",x="club_name",y="overall",xlabel="",figsize=(15,5))
plt.show()
结果是:
,
但我想进入前10列和底部10列以具有不同的颜色。例如,前10列是红色的,最后10列蓝色。我也希望我的图表具有一个传奇,可以解释哪种颜色对应于哪种颜色。 我希望有一种方法。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以迭代栏上并使用手动更新颜色。
You can iterate over the bars and use set_color() to update the color manually.
您可以在
绘图
中使用color
参数对颜色的列表不太好,但是如果您知道图中总是有20个俱乐部,那么它将执行诡计
You could use
color
parameter inplot
not very nice to make a list like that for color, but if you know that there is always 20 clubs in plot then it will do the trick