8000 Review · matplotlib/matplotlib@b6b9648 · GitHub
[go: up one dir, main page]

Skip to content

Commit b6b9648

Browse files
fmaussionefiring
authored andcommitted
Review
1 parent 754fd26 commit b6b9648

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

doc/users/whats_new/extend_kwarg_to_BoundaryNorm.rst

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

lib/matplotlib/colors.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,8 +1428,9 @@ def __init__(self, boundaries, ncolors, clip=False, extend='neither'):
14281428
above ``boundaries[-1]``. These are then converted to valid indices
14291429
by `Colormap.__call__`.
14301430
extend : str, optional
1431-
'neither', 'both', 'min', or 'max': select the colors out of
1432-
cmap so that the extensions are considered in the interpolation
1431+
'neither', 'both', 'min', or 'max': reserve the first (last) colors
1432+
of the colormap for data values below (above) the first (last)
1433+
boundary value.
14331434
14341435
Notes
14351436
-----

0 commit comments

Comments
 (0)
0