如何为matplotlib中的每条绘制线选择新颜色?
当我制作循环来创建我的行时,我对颜色有很小的问题,
您能帮我吗?
我想更改每行的颜色
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您之所以看不到颜色的原因是因为您正在运行2个循环,并且有几个值相同的数字(例如
16.5
和10
)。因此,线条被写在另一个上面。您可以通过这样更改代码来看到不同的颜色。但是请注意,只有特定y
值的最后颜色才能看到。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
and10
). 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 particulary
value will be seen.OUTPUT