如何为matplotlib中的每条绘制线选择新颜色?

发布于 2025-01-29 12:58:44 字数 520 浏览 4 评论 0原文

当我制作循环来创建我的行时,我对颜色有很小的问题,

您能帮我吗?

我想更改每行的颜色

data = {'Note':['3','3','3','4','4','4','5','5','5','1','2','2','1'],
        'Score':['10','10.1','17','17.5','16.5','14.3','10','10.1','17','17.5','16.5','16.5','16.5']}
  

数据框

df = pd.DataFrame(data)

创建

groups=df.groupby("Note")["Score"].min()
for color in ['r', 'b', 'g', 'k', 'm']:
    for i in groups.values:
        plt.axhline(y=i, color=color)
plt.show()

I have a small problem with the colors when I make a loop for to create my lines

Can you help me please?

I would like to change the color of each line

data = {'Note':['3','3','3','4','4','4','5','5','5','1','2','2','1'],
        'Score':['10','10.1','17','17.5','16.5','14.3','10','10.1','17','17.5','16.5','16.5','16.5']}
  

Create DataFrame

df = pd.DataFrame(data)

Create Plot by loop for

groups=df.groupby("Note")["Score"].min()
for color in ['r', 'b', 'g', 'k', 'm']:
    for i in groups.values:
        plt.axhline(y=i, color=color)
plt.show()

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

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

发布评论

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

评论(1

花想c 2025-02-05 12:58:45

您之所以看不到颜色的原因是因为您正在运行2个循环,并且有几个值相同的数字(例如16.510)。因此,线条被写在另一个上面。您可以通过这样更改代码来看到不同的颜色。但是请注意,只有特定y值的最后颜色才能看到。

data = {'Note':['3','3','3','4','4','4','5','5','5','1','2','2','1'],
        'Score':['10','10.1','17','17.5','16.5','14.3','10','10.1','17','17.5','16.5','16.5','16.5']}
df = pd.DataFrame(data)

groups=df.groupby("Note")["Score"].min()
print(group.values)
color = ['r', 'b', 'g', 'k', 'm']
j=0
for i in groups.values:
    plt.axhline(y=i, color=color[j%5])
    j=j+1
plt.show()

output

“输出线图”

The reason you are not seeing the colors is because you are running 2 for loops and there are several values which are the same number (like 16.5 and 10). So, the lines get written one on top of the other. You can see the different colors by changing you code like this. But do note that only the last color for a particular y value will be seen.

data = {'Note':['3','3','3','4','4','4','5','5','5','1','2','2','1'],
        'Score':['10','10.1','17','17.5','16.5','14.3','10','10.1','17','17.5','16.5','16.5','16.5']}
df = pd.DataFrame(data)

groups=df.groupby("Note")["Score"].min()
print(group.values)
color = ['r', 'b', 'g', 'k', 'm']
j=0
for i in groups.values:
    plt.axhline(y=i, color=color[j%5])
    j=j+1
plt.show()

OUTPUT

Output line chart

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