ShanZhengYang
ShanZhengYang

Reputation: 17631

How to set as default matplotlib contour plot to always label contours

Perhaps I'm missing something obvious in the documentation,

http://matplotlib.org/examples/pylab_examples/contour_demo.html

but when I first create a contour plot, there are labels for each contour line. However, matplotlib does not do this by default. Using the plot given in the demo, I generate more contour lines between 0.00 and 3.00 :

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

plt.figure()
levels = np.arange(0.00, 3.00, 0.25)
CS = plt.contour(X, Y, Z, levels=levels)
plt.clabel(CS, inline=1, fontsize=10)
plt.xlim(0, 3)
plt.ylim(0, 2)
plt.show()

which outputs

enter image description here

Each contour line is clearly labeled. Now, let's zoom in on a distinct region of this contour, i.e. ((0.5, 1.0), (0.5, 1.0))

plt.figure()
levels = np.arange(0.00, 3.00, 0.25)
CS = plt.contour(X, Y, Z, levels=levels)
plt.clabel(CS, inline=1, fontsize=10)
plt.xlim(0.5, 1.0)
plt.ylim(0.5, 1.0)
plt.show()

This output is clearly NOT labeled.

enter image description here

How can I set plt.contour to automatically label each and every contour line?

Upvotes: 3

Views: 1726

Answers (1)

lcit
lcit

Reputation: 306

You probably need to change x and y directly like so:

x = np.arange(0.5, 1.0, delta)
y = np.arange(0.5, 1.0, delta)

Upvotes: 2

Related Questions