Vitor Vitor - 3 months ago 12
Python Question

Matplotlib: How to plot an specfic curve without legend?

For example. I have two curves. One is the real curve and the other is just a dashed line to indicate an specific point:

import matplotlib.pyplot as plt

x2=[0,0.5,0.5]
y2=[0.5,0.5,0]
plt.plot(x2,y2,ls='dashed')

x1=[0,1]
y1=[0,1]
plt.plot(x1,y1)

plt.legend(['','y1'])

plt.show()


I don't want to show the first legend (I know that in this case I can just change the plot order to solve this problem)

enter image description here

cel cel
Answer

You can use the label keyword and let the legend() function generate the labels automatically.

x2=[0,0.5,0.5]
y2=[0.5,0.5,0]
plt.plot(x2,y2,ls='dashed')

x1=[0,1]
y1=[0,1]
plt.plot(x1,y1, label='y1')

plt.legend()

Which gives you this result:

enter image description here