Home > OS >  How to put contour labels as legend in a plot in Python?
How to put contour labels as legend in a plot in Python?

Time:06-30

I am plotting contours, using an example from this link (code and output plot below) enter image description here

I want the labels for each contour as a legend in the corner, not along the contour lines. Is there a way?

CodePudding user response:

Try this:

delta = 0.15
x = np.arange(1.5, 2.5, delta)
y = np.arange(1.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(X - Y))
  
CS1 = plt.contour(X, Y, Z)
   
nm, lbl = CS1.legend_elements()
lbl_ = [i for i in range(1, len(lbl))]
plt.legend(nm, lbl_) 

plt.title('matplotlib.pyplot.contour() Example')
plt.show()

enter image description here

CodePudding user response:

  1. This post enter image description here

  • Related