Skip to content

Commit ee2d7f7

Browse files
committed
Merge pull request matplotlib#1229 from ajdawson/colorbar-triangles
NF - option to make colorbar extensions rectangles
2 parents 880061d + 08c1148 commit ee2d7f7

File tree

6 files changed

+95
-10
lines changed

6 files changed

+95
-10
lines changed

doc/api/api_changes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ help figure out possible sources of the changes you are experiencing.
1111
For new features that were added to matplotlib, please see
1212
:ref:`whats-new`.
1313

14+
15+
Changes in 1.3.x
16+
================
17+
18+
* A new keyword *extendrect* in :meth:`~matplotlib.pyplot.colorbar` and
19+
:class:`~matplotlib.colorbar.ColorbarBase` allows one to control the shape
20+
of colorbar extensions.
21+
1422
Changes in 1.2.x
1523
================
1624

doc/users/whats_new.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ revision, see the :ref:`github-stats`.
1515
versions 2.4 to 2.7. matplotlib 1.2 and later require
1616
versions 2.6, 2.7, and 3.1 and higher.
1717

18+
.. _whats-new-1-3:
19+
20+
new in matplotlib-1.3
21+
=====================
22+
23+
Rectangular colorbar extensions
24+
-------------------------------
25+
Andrew Dawson added a new keyword argument *extendrect* to
26+
:meth:`~matplotlib.pyplot.colorbar` to optionally make colorbar
27+
extensions rectangular instead of triangular.
28+
29+
1830
.. _whats-new-1-2:
1931

2032
new in matplotlib-1.2

lib/matplotlib/colorbar.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@
8282
be given, indicating the lengths of the minimum and
8383
maximum colorbar extensions respectively as a
8484
fraction of the interior colorbar length.
85+
*extendrect* [ *False* | *True* ]
86+
If *False* the minimum and maximum colorbar extensions
87+
will be triangular (the default). If *True* the
88+
extensions will be rectangular.
8589
*spacing* [ 'uniform' | 'proportional' ]
8690
Uniform spacing gives each discrete color the same
8791
space; proportional makes the space proportional to
@@ -258,6 +262,7 @@ def __init__(self, ax, cmap=None,
258262
drawedges=False,
259263
filled=True,
260264
extendfrac=None,
265+
extendrect=False,
261266
):
262267
self.ax = ax
263268
self._patch_ax()
@@ -276,6 +281,7 @@ def __init__(self, ax, cmap=None,
276281
self.drawedges = drawedges
277282
self.filled = filled
278283
self.extendfrac = extendfrac
284+
self.extendrect = extendrect
279285
self.solids = None
280286
self.lines = list()
281287
self.outline = None
@@ -773,9 +779,9 @@ def _mesh(self):
773779
y = self._proportional_y()
774780
self._y = y
775781
X, Y = np.meshgrid(x, y)
776-
if self._extend_lower():
782+
if self._extend_lower() and not self.extendrect:
777783
X[0, :] = 0.5
778-
if self._extend_upper():
784+
if self._extend_upper() and not self.extendrect:
779785
X[-1, :] = 0.5
780786
return X, Y
781787

4.18 KB
Loading
4.16 KB
Loading

lib/matplotlib/tests/test_colorbar.py

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,69 @@
66
from matplotlib.colorbar import ColorbarBase
77

88

9-
def _colorbar_extensions(spacing):
9+
def _get_cmap_norms():
10+
"""
11+
Define a colormap and appropriate norms for each of the four
12+
possible settings of the extend keyword.
1013
14+
Helper function for _colorbar_extension_shape and
15+
colorbar_extension_length.
16+
"""
1117
# Create a color map and specify the levels it represents.
1218
cmap = get_cmap("RdBu", lut=5)
1319
clevs = [-5., -2.5, -.5, .5, 1.5, 3.5]
14-
1520
# Define norms for the color maps.
1621
norms = dict()
1722
norms['neither'] = BoundaryNorm(clevs, len(clevs)-1)
1823
norms['min'] = BoundaryNorm([-10]+clevs[1:], len(clevs)-1)
1924
norms['max'] = BoundaryNorm(clevs[:-1]+[10], len(clevs)-1)
2025
norms['both'] = BoundaryNorm([-10]+clevs[1:-1]+[10], len(clevs)-1)
26+
return cmap, norms
27+
28+
29+
def _colorbar_extension_shape(spacing):
30+
'''
31+
Produce 4 colorbars with rectangular extensions for either uniform
32+
or proportional spacing.
2133
34+
Helper function for test_colorbar_extension_shape.
35+
'''
36+
# Get a colormap and appropriate norms for each extension type.
37+
cmap, norms = _get_cmap_norms()
38+
# Create a figure and adjust whitespace for subplots.
39+
fig = plt.figure()
40+
fig.subplots_adjust(hspace=4)
41+
for i, extension_type in enumerate(('neither', 'min', 'max', 'both')):
42+
# Get the appropriate norm and use it to get colorbar boundaries.
43+
norm = norms[extension_type]
44+
boundaries = values = norm.boundaries
45+
# Create a subplot.
46+
cax = fig.add_subplot(4, 1, i+1)
47+
# Turn off text and ticks.
48+
for item in cax.get_xticklabels() + cax.get_yticklabels() +\
49+
cax.get_xticklines() + cax.get_yticklines():
50+
item.set_visible(False)
51+
# Generate the colorbar.
52+
cb = ColorbarBase(cax, cmap=cmap, norm=norm,
53+
boundaries=boundaries, values=values,
54+
extend=extension_type, extendrect=True,
55+
orientation='horizontal', spacing=spacing)
56+
# Return the figure to the caller.
57+
return fig
58+
59+
60+
def _colorbar_extension_length(spacing):
61+
'''
62+
Produce 12 colorbars with variable length extensions for either
63+
uniform or proportional spacing.
64+
65+
Helper function for test_colorbar_extension_length.
66+
'''
67+
# Get a colormap and appropriate norms for each extension type.
68+
cmap, norms = _get_cmap_norms()
2269
# Create a figure and adjust whitespace for subplots.
2370
fig = plt.figure()
2471
fig.subplots_adjust(hspace=.6)
25-
2672
for i, extension_type in enumerate(('neither', 'min', 'max', 'both')):
2773
# Get the appropriate norm and use it to get colorbar boundaries.
2874
norm = norms[extension_type]
@@ -39,20 +85,33 @@ def _colorbar_extensions(spacing):
3985
boundaries=boundaries, values=values,
4086
extend=extension_type, extendfrac=extendfrac,
4187
orientation='horizontal', spacing=spacing)
42-
4388
# Return the figure to the caller.
4489
return fig
4590

4691

92+
@image_comparison(
93+
baseline_images=['colorbar_extensions_shape_uniform',
94+
'colorbar_extensions_shape_proportional'],
95+
extensions=['png'])
96+
def test_colorbar_extension_shape():
97+
'''Test rectangular colorbar extensions.'''
98+
# Use default params so matplotlibrc doesn't cause the test to fail.
99+
rcParams.update(rcParamsDefault)
100+
# Create figures for uniform and proportionally spaced colorbars.
101+
fig1 = _colorbar_extension_shape('uniform')
102+
fig2 = _colorbar_extension_shape('proportional')
103+
104+
47105
@image_comparison(
48106
baseline_images=['colorbar_extensions_uniform', 'colorbar_extensions_proportional'],
49107
extensions=['png'])
50-
def test_colorbar_extensions():
51-
# Use default params so .matplotlibrc doesn't cause the test to fail.
108+
def test_colorbar_extension_length():
109+
'''Test variable length colorbar extensions.'''
110+
# Use default params so matplotlibrc doesn't cause the test to fail.
52111
rcParams.update(rcParamsDefault)
53112
# Create figures for uniform and proportionally spaced colorbars.
54-
fig1 = _colorbar_extensions('uniform')
55-
fig2 = _colorbar_extensions('proportional')
113+
fig1 = _colorbar_extension_length('uniform')
114+
fig2 = _colorbar_extension_length('proportional')
56115

57116

58117
if __name__ == '__main__':

0 commit comments

Comments
 (0)