-2

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()
DAMIEN
  • 17
  • 3
  • I don't know anything about pandas. However, through my eyehairs is seems that you are giving one of the solutions: looping over colors/lines. Or is there a 'nested' problem? It could help to have a figure with the actual and desired output and/or a Python/matplotlib-only example – Tom de Geus May 15 '22 at 12:39

1 Answers1

1

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

Redox
  • 9,321
  • 5
  • 9
  • 26