如何为Matplotlib条形图中的特定列设置不同的颜色?

发布于 2025-02-02 21:56:47 字数 633 浏览 1 评论 0 原文

我创建了一个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列蓝色。我也希望我的图表具有一个传奇,可以解释哪种颜色对应于哪种颜色。 我希望有一种方法。

I created a bar chart of 10 football clubs with highest and 10 with the lowest FIFA ratings.
I plotted both on the same figure this way:

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()

The result is this:
[image]

But I want to top 10 and bottom 10 columns to have different colors. For example first 10 columns to be red and last 10 blue color. Also I want my chart to have a legend that explains which color corresponds to which.
I hope there is a way of doing this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城花音 2025-02-09 21:56:47

您可以迭代栏上并使用手动更新颜色。

import matplotlib
import matplotlib.pyplot as plt


ax = unioned.plot(kind="bar", x="club_name", y="overall", xlabel="", figsize=(15,5))

# Highest ratings
for i in range(0, 10):
    ax.get_children()[i].set_color("red")

# Lowest ratings
for i in range(10, 20):
    ax.get_children()[i].set_color("blue")

legends = [
    matplotlib.patches.Patch(color="red", label="Highest ratings"),
    matplotlib.patches.Patch(color="blue", label="Lowest ratings"),
]
ax.legend(handles=legends, prop={"size": 20})

plt.show()

You can iterate over the bars and use set_color() to update the color manually.

import matplotlib
import matplotlib.pyplot as plt


ax = unioned.plot(kind="bar", x="club_name", y="overall", xlabel="", figsize=(15,5))

# Highest ratings
for i in range(0, 10):
    ax.get_children()[i].set_color("red")

# Lowest ratings
for i in range(10, 20):
    ax.get_children()[i].set_color("blue")

legends = [
    matplotlib.patches.Patch(color="red", label="Highest ratings"),
    matplotlib.patches.Patch(color="blue", label="Lowest ratings"),
]
ax.legend(handles=legends, prop={"size": 20})

plt.show()
虫児飞 2025-02-09 21:56:47

您可以在绘图中使用 color 参数

unioned.plot(kind="bar",x="club_name",y="overall",xlabel="",figsize=(15,5), color=[*['red']*10, *['green']*10])

对颜色的列表不太好,但是如果您知道图中总是有20个俱乐部,那么它将执行诡计

You could use color parameter in plot

unioned.plot(kind="bar",x="club_name",y="overall",xlabel="",figsize=(15,5), color=[*['red']*10, *['green']*10])

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文