Home > Back-end >  Matplot lib line plot legend shows wrong color code for the lines
Matplot lib line plot legend shows wrong color code for the lines

Time:11-18

The mat plot lib gives legend of line graph, but the color doesn't match. Here is a code to illustrate the issue.

import matplotlib.pyplot as plt 
import math

d = {'Training': [270,180,160,150,135,130,120,110,100,95],'Validation': [290,220,200,180,170,173,175,185,188,195]
    ,'Validation Lr Drecay': [290,220,200,180,170,170,168,166,170,175]}
df = pd.DataFrame(data=d)
import matplotlib.pyplot as plt
df.plot( )

plt.plot(df)

new_list = range(1,10)

plt.xticks(new_list)
#plt.yticks(range(1,5) * 10   100)
plt.grid()
plt.title('model accuracy')
plt.ylabel('Error')
plt.xlabel('Iteration')

plt.show()

enter image description here

CodePudding user response:

Here is the simple solution. Remove plt.plot(df).

It occurs because df.plot() makes plot with blue, orange, and green colors with legend, but plt.plot(df) makes new plot with other colors without legend.

CodePudding user response:

Gilseung Ahn’s solution is fast way. another solution is: delete df.plot() and plt.plot(df), then add these lines to your code:

plt.plot(df['Training'])
plt.plot(df['Validation'])
plt.plot(df['Validation Lr Drecay'])
  • Related