0

i have problem, i want to draw multi line color diagram in python, i don't know how can i do that, my data frame like this:

frame  |  name     |  color  | max_val

0      |  sadness  |   c     |   0.07

24     |  sadness  |   c     |   0.054

48     |  neutral  |   k     |   0.8

etc..

my script is:

 enomy = {
"anger":"r",
"disgust":"g",
"fear":"m",
"happiness":"y",
"sadness":"c",
"surprise":"b",
"neutral":"k",
}
x = df["Max_value"]
y = df["frame"]
z = []
for i in range(len(x)):
    z.append(enomy[list(df["name"])[i]])

fig, ax = plt.subplots()
for j in range(len(x)):
    plt.plot(y, x, color=z[j])

plt.show()

and this plot is like that: enter image description here

how i can solve this and draw multi line plot?

mahdi gadget
  • 83
  • 1
  • 6
  • 1
    I hope [this 3rd answer](https://stackoverflow.com/questions/35372993/python-matplotlib-multicolor-line) and the [official reference](https://matplotlib.org/devdocs/gallery/lines_bars_and_markers/multicolored_line.html) will help you! – r-beginners Dec 20 '21 at 13:02

1 Answers1

1

The problem seems to be that you are drawing the whole set of lines each time with a new color inside for j. A simple solution is draw one part of the whole curve each time, for example:

for j in range(len(x)-1):
    plt.plot(y[j:j+2], x[j:j+2], color=z[j])

(I can't be sure if you want the color of each line to be associated with the start or end point. The above code colors each line according to the starting frame.)