Skip to content

Commit 67a51cb

Browse files
domspadjenshnielsen
authored andcommitted
pylab to plt and np
1 parent c9c1631 commit 67a51cb

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

examples/pylab_examples/contour_image.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,34 @@
88
desired. In particular, note the usage of the "origin" and "extent"
99
keyword arguments to imshow and contour.
1010
'''
11-
from pylab import *
11+
import matplotlib.pyplot as plt
12+
import numpy as np
13+
from matplotlib import mlab, cm
1214

1315
# Default delta is large because that makes it fast, and it illustrates
1416
# the correct registration between image and contours.
1517
delta = 0.5
1618

1719
extent = (-3, 4, -4, 3)
1820

19-
x = arange(-3.0, 4.001, delta)
20-
y = arange(-4.0, 3.001, delta)
21-
X, Y = meshgrid(x, y)
22-
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
23-
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
21+
x = np.arange(-3.0, 4.001, delta)
22+
y = np.arange(-4.0, 3.001, delta)
23+
X, Y = np.meshgrid(x, y)
24+
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
25+
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
2426
Z = (Z1 - Z2) * 10
2527

26-
levels = arange(-2.0, 1.601, 0.4) # Boost the upper limit to avoid truncation errors.
28+
levels = np.arange(-2.0, 1.601, 0.4) # Boost the upper limit to avoid truncation errors.
2729

2830
norm = cm.colors.Normalize(vmax=abs(Z).max(), vmin=-abs(Z).max())
2931
cmap = cm.PRGn
3032

31-
figure()
33+
plt.figure()
3234

3335

34-
subplot(2, 2, 1)
36+
plt.subplot(2, 2, 1)
3537

36-
cset1 = contourf(X, Y, Z, levels,
38+
cset1 = plt.contourf(X, Y, Z, levels,
3739
cmap=cm.get_cmap(cmap, len(levels) - 1),
3840
norm=norm,
3941
)
@@ -45,7 +47,7 @@
4547
# contour separately; don't try to change the edgecolor or edgewidth
4648
# of the polygons in the collections returned by contourf.
4749
# Use levels output from previous call to guarantee they are the same.
48-
cset2 = contour(X, Y, Z, cset1.levels,
50+
cset2 = plt.contour(X, Y, Z, cset1.levels,
4951
colors='k',
5052
hold='on')
5153
# We don't really need dashed contour lines to indicate negative
@@ -57,49 +59,49 @@
5759
# to set up an array of colors and linewidths.
5860
# We are making a thick green line as a zero contour.
5961
# Specify the zero level as a tuple with only 0 in it.
60-
cset3 = contour(X, Y, Z, (0,),
62+
cset3 = plt.contour(X, Y, Z, (0,),
6163
colors='g',
6264
linewidths=2,
6365
hold='on')
64-
title('Filled contours')
65-
colorbar(cset1)
66+
plt.title('Filled contours')
67+
plt.colorbar(cset1)
6668
#hot()
6769

6870

69-
subplot(2, 2, 2)
71+
plt.subplot(2, 2, 2)
7072

71-
imshow(Z, extent=extent, cmap=cmap, norm=norm)
72-
v = axis()
73-
contour(Z, levels, hold='on', colors='k',
73+
plt.imshow(Z, extent=extent, cmap=cmap, norm=norm)
74+
v = plt.axis()
75+
plt.contour(Z, levels, hold='on', colors='k',
7476
origin='upper', extent=extent)
75-
axis(v)
76-
title("Image, origin 'upper'")
77+
plt.axis(v)
78+
plt.title("Image, origin 'upper'")
7779

78-
subplot(2, 2, 3)
80+
plt.subplot(2, 2, 3)
7981

80-
imshow(Z, origin='lower', extent=extent, cmap=cmap, norm=norm)
81-
v = axis()
82-
contour(Z, levels, hold='on', colors='k',
82+
plt.imshow(Z, origin='lower', extent=extent, cmap=cmap, norm=norm)
83+
v = plt.axis()
84+
plt.contour(Z, levels, hold='on', colors='k',
8385
origin='lower', extent=extent)
84-
axis(v)
85-
title("Image, origin 'lower'")
86+
plt.axis(v)
87+
plt.title("Image, origin 'lower'")
8688

87-
subplot(2, 2, 4)
89+
plt.subplot(2, 2, 4)
8890

8991
# We will use the interpolation "nearest" here to show the actual
9092
# image pixels.
9193
# Note that the contour lines don't extend to the edge of the box.
9294
# This is intentional. The Z values are defined at the center of each
9395
# image pixel (each color block on the following subplot), so the
9496
# domain that is contoured does not extend beyond these pixel centers.
95-
im = imshow(Z, interpolation='nearest', extent=extent, cmap=cmap, norm=norm)
96-
v = axis()
97-
contour(Z, levels, hold='on', colors='k',
97+
im = plt.imshow(Z, interpolation='nearest', extent=extent, cmap=cmap, norm=norm)
98+
v = plt.axis()
99+
plt.contour(Z, levels, hold='on', colors='k',
98100
origin='image', extent=extent)
99-
axis(v)
100-
ylim = get(gca(), 'ylim')
101-
setp(gca(), ylim=ylim[::-1])
102-
title("Image, origin from rc, reversed y-axis")
103-
colorbar(im)
101+
plt.axis(v)
102+
ylim = plt.get(plt.gca(), 'ylim')
103+
plt.setp(plt.gca(), ylim=ylim[::-1])
104+
plt.title("Image, origin from rc, reversed y-axis")
105+
plt.colorbar(im)
104106

105-
show()
107+
plt.show()

0 commit comments

Comments
 (0)