【问题标题】:Change plot color according to the values from array [duplicate]根据数组中的值更改绘图颜色[重复]
【发布时间】: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


【解决方案1】:
  • 主要问题将作为此answer 的此question 的副本关闭
    • 代码在副本中有解释。
  • 当一个问题被标记为重复并且您不同意时,您有责任用代码显示您尝试合并重复的确切方式,以及哪些问题不起作用。李>
  • SO 是问题和答案的存储库,可用作回答新问题的参考。当现有问题/答案中的代码通过代码回答问题时,由您来完成工作。
  • 由于它是重复的,因此此答案已添加为社区 wiki。
from matplotlib.lines import Line2D
import pandas as pd
import matplotlib.pyplot as plt

# set up the dataframe to match the duplicate
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]

df = pd.DataFrame({'val': random_emotions})

# map values is covered in duplicate
emotion_dict = {0: 'Angry', 1: 'Fear', 2: 'Happy', 3: 'Neutral', 4: 'Sad'}
color_dict = {0: 'k', 1: 'r', 2: 'b', 3: 'y', 4: 'g'}
df['emotion'] = df.val.map(emotion_dict)
df['color'] = df.val.map(color_dict)

# everything else from here is a duplicated

df['change'] = df.val.ne(df.val.shift().bfill()).astype(int)
df['subgroup'] = df['change'].cumsum()

df.index += df['subgroup'].values

first_i_of_each_group = df[df['change'] == 1].index
for i in first_i_of_each_group:
    # Copy next group's first row to current group's last row
    df.loc[i-1] = df.loc[i]
    # But make this new row part of the current group
    df.loc[i-1, 'subgroup'] = df.loc[i-2, 'subgroup']
# Don't need the change col anymore
df.drop('change', axis=1, inplace=True)
df.sort_index(inplace=True)
# Create duplicate indexes at each subgroup border to ensure the plot is continuous.
df.index -= df['subgroup'].values

fig, ax = plt.subplots(figsize=(15, 4))
for k, g in df.groupby('subgroup'):
    g.plot(ax=ax, y='val', color=g['color'].values[0], marker='.', legend=False, xticks=df.index)

ax.margins(x=0)

# create custom legend is covered in duplicate
custom_lines = [Line2D([0], [0], color=color, lw=4) for color in color_dict.values()]
_ = ax.legend(title='Emotion', handles=custom_lines, labels=emotion_dict.values(), bbox_to_anchor=(1, 1.02), loc='upper left')

# display(df.T)
             0      1      2      3      4      5      6     7     7      8      8        9        9      10     10    11    11     12     12    13    13     14     14       15       15     16     16     17     18     19    20    20    21     22     22       23       23       24       25       26       27       28   29   29   30   31   32   33     34     34    35    35     36     36     37    38    38     39     39       40       40   41   41     42     42     43     44     45     46
val           0      0      0      0      0      0      0     1     1      2      2        3        3      2      2     1     1      2      2     1     1      2      2        3        3      2      2      2      2      2     1     1     1      2      2        3        3        3        3        3        3        3    4    4    4    4    4    4      2      2     1     1      2      2      2     1     1      2      2        3        3    4    4      0      0      0      0      0      0
emotion   Angry  Angry  Angry  Angry  Angry  Angry  Angry  Fear  Fear  Happy  Happy  Neutral  Neutral  Happy  Happy  Fear  Fear  Happy  Happy  Fear  Fear  Happy  Happy  Neutral  Neutral  Happy  Happy  Happy  Happy  Happy  Fear  Fear  Fear  Happy  Happy  Neutral  Neutral  Neutral  Neutral  Neutral  Neutral  Neutral  Sad  Sad  Sad  Sad  Sad  Sad  Happy  Happy  Fear  Fear  Happy  Happy  Happy  Fear  Fear  Happy  Happy  Neutral  Neutral  Sad  Sad  Angry  Angry  Angry  Angry  Angry  Angry
color         k      k      k      k      k      k      k     r     r      b      b        y        y      b      b     r     r      b      b     r     r      b      b        y        y      b      b      b      b      b     r     r     r      b      b        y        y        y        y        y        y        y    g    g    g    g    g    g      b      b     r     r      b      b      b     r     r      b      b        y        y    g    g      k      k      k      k      k      k
subgroup      0      0      0      0      0      0      0     0     1      1      2        2        3      3      4     4     5      5      6     6     7      7      8        8        9      9     10     10     10     10    10    11    11     11     12       12       13       13       13       13       13       13   13   14   14   14   14   14     14     15    15    16     16     17     17    17    18     18     19       19       20   20   21     21     22     22     22     22     22

【讨论】:

  • 非常感谢您的帮助,我会改进我的问题,并在下次展示我对副本所做的一切。
  • @sjsu 不客气。
猜你喜欢
  • 2012-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-10
相关资源
最近更新 更多