0

Okay this might be easy even I search on web but could not get it. Basically i want to add the two different labels to my plot and this is my line of code for that

plt.plot(x[:,1],y,'ro',x[:,1],Line_fit,'b',linewidth=0.5,markersize=4,label="training data") # plot the data 
plt.legend(loc="upper left")

but I am getting following result in which has same labels for both the plot.as following

enter image description here

Even I tried this

plt.plot(x[:,1],y,'ro',x[:,1],Line_fit,'b',linewidth=0.5,markersize=4,label="training data",label="Linear Regression") # plot the data 

but give the error:

SyntaxError: keyword argument repeated

This link guide for the simple way but here plt.plot()had used twice in the accepted answer.My question is how can i do it the same thing in single line code as I did in my code ?

Anil Sarode
  • 175
  • 13

1 Answers1

1

You need two lines. One for the plot, one for the legend.

plt.plot(x[:,1], y, 'ro', x[:,1], Line_fit, 'b', linewidth=0.5, markersize=4)
plt.legend(["training data", "Linear Regression"], loc="upper left")
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712