Vitor Abella
Vitor Abella

Reputation: 1363

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

Upvotes: 2

Views: 601

Answers (1)

cel
cel

Reputation: 31349

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

Upvotes: 4

Related Questions