【发布时间】:2021-11-27 19:14:28
【问题描述】:
我确实尝试了与我的问题类似的其他解决方案,但没有成功, python: how to plot one line in different colors
Color by Column Values in Matplotlib
pandas plot one line graph with color change on column
我希望情节在值发生变化时改变颜色,例如,如果情绪为0,它将保持黑色,如果值更改为1,则颜色为红色,如果值为2,则颜色将是蓝色等。到目前为止我所取得的进展附在这个问题上,提前谢谢你。
random_emotions = [0,0,0,0,0,0,0,1,2,3,2,1,2,1,
2,3,2,2,2,2,1,1,2,3,3,3,3,3,3,4,
4,4,4,4,2,1,2,2,1,2,3,4,0,0,0,0,0]
random_emotions = np.array(random_emotions)
EmotionsInNumber = random_emotions
x = np.array(list(range(0,len(EmotionsInNumber))))
Angry = np.ma.masked_where(EmotionsInNumber == 0,EmotionsInNumber)
Fear = np.ma.masked_where(EmotionsInNumber == 1,EmotionsInNumber)
Happy = np.ma.masked_where(EmotionsInNumber == 2,EmotionsInNumber)
Neutral = np.ma.masked_where(EmotionsInNumber == 3, EmotionsInNumber)
Sad = np.ma.masked_where(EmotionsInNumber == 4,EmotionsInNumber)
fig, ax = plt.subplots()
ax.plot(x, Angry,linewidth = 4, color = 'black')
ax.plot(x, Fear,linewidth = 4, color = 'red')
ax.plot(x, Happy,linewidth = 4, color = 'blue')
ax.plot(x, Neutral,linewidth = 4, color = 'yellow')
ax.plot(x, Sad,linewidth = 4, color = 'green')
ax.legend(['Angry','Fear','Happy','Neutral','Sad',])
ax.set_title("Emotion Report of ")
plt.show()
This is the result that I am getting
颜色没有相应改变,传说是错误的,我不知道如何解决这个问题。
matplotlib color line by "value" [duplicate] 这个 'matplotlib color line by "value" [duplicate]' 是我得到的最接近的,但是当索引 1 和 5 上的颜色变为青色时,蓝色应该是空的,但它一直在绘制蓝色和青色。这是因为数据框按“颜色”分组,但不应在图表上的 1 和 5 上绘制蓝色,在 2、3、4 上绘制青色。
【问题讨论】:
-
您的其他 question 已关闭,因为重复项显示了执行此操作的各种方法。这个问题与上一个问题完全相同,并且没有显示出使用重复项中的任何信息的努力。 SO 不是编码服务,并且声明重复项不起作用是不够的。您的问题必须显示您尝试了哪些重复项,以及它是如何不起作用的,否则,问题很可能会再次关闭。
-
他们没有工作怎么办?展示你做了什么
标签: python arrays matplotlib plot colors