10000 DOC: convert example to use OO interface · matplotlib/matplotlib@50e33f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 50e33f5

Browse files
committed
DOC: convert example to use OO interface
1 parent 2dfc9ec commit 50e33f5

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

examples/pylab_examples/image_masked.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
The second subplot illustrates the use of BoundaryNorm to
55
get a filled contour effect.
66
"""
7-
7+
from copy import copy
88
from numpy import ma
99
import matplotlib.colors as colors
1010
import matplotlib.pyplot as plt
1111
import matplotlib.mlab as mlab
1212
import numpy as np
1313

14+
# compute some interesting data
1415
delta = 0.025
1516
x = y = np.arange(-3.0, 3.0, delta)
1617
X, Y = np.meshgrid(x, y)
@@ -19,7 +20,8 @@
1920
Z = 10*(Z2 - Z1) # difference of Gaussians
2021

2122
# Set up a colormap:
22-
palette = plt.cm.gray
23+
# use copy so that we do not mutate the global colormap instance
24+
palette = copy(plt.cm.gray)
2325
palette.set_over('r', 1.0)
2426
palette.set_under('g', 1.0)
2527
palette.set_bad('b', 1.0)
@@ -35,22 +37,25 @@
3537
# range to which the regular palette color scale is applied.
3638
# Anything above that range is colored based on palette.set_over, etc.
3739

38-
plt.subplot(1, 2, 1)
39-
im = plt.imshow(Zm, interpolation='bilinear',
40+
# set up the axes
41+
fig, (ax1, ax2) = plt.subplots(1, 2)
42+
43+
# plot using 'continuous' color map
44+
im = ax1.imshow(Zm, interpolation='bilinear',
4045
cmap=palette,
4146
norm=colors.Normalize(vmin=-1.0, vmax=1.0, clip=False),
4247
origin='lower', extent=[-3, 3, -3, 3])
43-
plt.title('Green=low, Red=high, Blue=bad')
44-
plt.colorbar(im, extend='both', orientation='horizontal', shrink=0.8)
48+
ax1.set_title('Green=low, Red=high, Blue=bad')
49+
fig.colorbar(im, extend='both', orientation='horizontal', shrink=0.8, ax=ax1)
4550

46-
plt.subplot(1, 2, 2)
47-
im = plt.imshow(Zm, interpolation='nearest',
51+
# plot using 'discrete' color map
52+
im = ax2.imshow(Zm, interpolation='nearest',
4853
cmap=palette,
4954
norm=colors.BoundaryNorm([-1, -0.5, -0.2, 0, 0.2, 0.5, 1],
5055
ncolors=256, clip=False),
5156
origin='lower', extent=[-3, 3, -3, 3])
52-
plt.title('With BoundaryNorm')
53-
plt.colorbar(im, extend='both', spacing='proportional',
54-
orientation='horizontal', shrink=0.8)
57+
ax2.set_title('With BoundaryNorm')
58+
fig.colorbar(im, extend='both', spacing='proportional',
59+
orientation='horizontal', shrink=0.8, ax=ax2)
5560

5661
plt.show()

0 commit comments

Comments
 (0)
0