@@ -11,31 +11,36 @@ Example
1111```````
1212::
1313
14- from matplotlib import pyplot as plt
15- import matplotlib as mpl
16-
17- # Make a figure and axes with dimensions as desired.
18- fig = plt.figure(figsize=(8, 3))
19- ax1 = fig.add_axes([0.05, 0.7, 0.9, 0.2])
20- ax2 = fig.add_axes([0.05, 0.2, 0.9, 0.2])
21-
22- # Set the colormap and bounds
23- bounds = [-1, 2, 5, 7, 12, 15]
24- cmap = mpl.cm.get_cmap('viridis')
25-
26- # Default behavior
27- norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
28- cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap,
29- norm=norm,
30- extend='both',
31- orientation='horizontal')
32- cb1.set_label('Default BoundaryNorm ouput')
33-
34- # New behavior
35- norm = mpl.colors.BoundaryNorm(bounds, cmap.N, extend='both')
36- cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap,
37- norm=norm,
38- orientation='horizontal')
39- cb2.set_label("With new extend='both' keyword")
40-
41- plt.show()
14+ import matplotlib.pyplot as plt
15+ from matplotlib.colors import BoundaryNorm
16+ import numpy as np
17+
18+ # Make the data
19+ dx, dy = 0.05, 0.05
20+ y, x = np.mgrid[slice(1, 5 + dy, dy),
21+ slice(1, 5 + dx, dx)]
22+ z = np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
23+ z = z[:-1, :-1]
24+
25+ # Z roughly varies between -1 and +1
26+ # my levels are chosen so that the color bar should be extended
27+ levels = [-0.8, -0.5, -0.2, 0.2, 0.5, 0.8]
28+ cmap = plt.get_cmap('PiYG')
29+
30+ # Before this change
31+ plt.subplot(2, 1, 1)
32+ norm = BoundaryNorm(levels, ncolors=cmap.N)
33+ im = plt.pcolormesh(x, y, z, cmap=cmap, norm=norm)
34+ plt.colorbar(extend='both')
35+ plt.axis([x.min(), x.max(), y.min(), y.max()])
36+ plt.title('pcolormesh with extended colorbar')
37+
38+ # With the new keyword
39+ norm = BoundaryNorm(levels, ncolors=cmap.N, extend='both')
40+ plt.subplot(2, 1, 2)
41+ im = plt.pcolormesh(x, y, z, cmap=cmap, norm=norm)
42+ plt.colorbar() # note that the colorbar is updated accordingly
43+ plt.axis([x.min(), x.max(), y.min(), y.max()])
44+ plt.title('pcolormesh with extended BoundaryNorm')
45+
46+ plt.show()
0 commit comments