Skip to content

Commit 32aaee4

Browse files
committed
MNT: more control of colorbar with CountourSet
In the `ColorBar` constructor values are extracted from the mappable if it is a ContourSet. Instead of always overwriting user-input only, respect user input if given. If the user passes in kwargs which can be extracted from a ContourSet raise TypeError instead of silently ignoring user input.
1 parent c94c282 commit 32aaee4

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
`ColorBar` raises TypeError rather than ignore user input
2+
`````````````````````````````````````````````````````````
3+
4+
In ``__init__`` `ColorBar` ignores user supplied ``cmap`` and ``norm``
5+
keyword arguments in favor of retrieving those values from the
6+
``mappable`` passed in. If those keywords are supplied, `ColorBar`
7+
will now raise a `TypeError` rather than silently dropping user supplied
8+
input.
9+
10+
If ``mappable`` is a `contour.ContourSet` then a `TypeError` will be raised
11+
if the user provides any of
12+
``('alpha', 'boundaries', 'values', 'extend', 'filled')``

examples/pylab_examples/contour_demo.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
CS = plt.contour(Z, levels,
8181
origin='lower',
8282
linewidths=2,
83-
extent=(-3, 3, -2, 2))
83+
extent=(-3, 3, -2, 2), extend='both')
8484

8585
# Thicken the zero contour.
8686
zc = CS.collections[6]
@@ -92,7 +92,7 @@
9292
fontsize=14)
9393

9494
# make a colorbar for the contour lines
95-
CB = plt.colorbar(CS, shrink=0.8, extend='both')
95+
CB = plt.colorbar(CS, shrink=0.8)
9696

9797
plt.title('Lines with colorbar')
9898
#plt.hot() # Now change the colormap for the contour lines and colorbar

lib/matplotlib/colorbar.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -889,18 +889,21 @@ def __init__(self, ax, mappable, **kw):
889889
mappable.autoscale_None()
890890

891891
self.mappable = mappable
892+
kw = cbook.normalize_kwargs(kw, forbidden=('cmap', 'norm'))
892893
kw['cmap'] = cmap = mappable.cmap
893-
kw['norm'] = norm = mappable.norm
894+
kw['norm'] = mappable.norm
894895

895896
if isinstance(mappable, contour.ContourSet):
896897
CS = mappable
898+
override_list = ('alpha', 'boundaries', 'values', 'extend',
899+
'filled')
900+
kw = cbook.normalize_kwargs(kw, forbidden=override_list)
897901
kw['alpha'] = mappable.get_alpha()
898902
kw['boundaries'] = CS._levels
899903
kw['values'] = CS.cvalues
900904
kw['extend'] = CS.extend
901-
#kw['ticks'] = CS._levels
902-
kw.setdefault('ticks', ticker.FixedLocator(CS.levels, nbins=10))
903905
kw['filled'] = CS.filled
906+
kw.setdefault('ticks', ticker.FixedLocator(CS.levels, nbins=10))
904907
ColorbarBase.__init__(self, ax, **kw)
905908
if not CS.filled:
906909
self.add_lines(CS)
@@ -909,7 +912,7 @@ def __init__(self, ax, mappable, **kw):
909912
kw.setdefault('extend', cmap.colorbar_extend)
910913

911914
if isinstance(mappable, martist.Artist):
912-
kw['alpha'] = mappable.get_alpha()
915+
kw.setdefault('alpha', mappable.get_alpha())
913916

914917
ColorbarBase.__init__(self, ax, **kw)
915918

lib/matplotlib/tests/test_colorbar.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,13 @@ def test_colorbar_closed_patch():
272272
values = np.linspace(0, 10, 5)
273273

274274
with rc_context({'axes.linewidth': 16}):
275-
plt.colorbar(im, cax=ax2, cmap=cmap, orientation='horizontal',
275+
plt.colorbar(im, cax=ax2, orientation='horizontal',
276276
extend='both', extendfrac=0.5, values=values)
277-
plt.colorbar(im, cax=ax3, cmap=cmap, orientation='horizontal',
277+
plt.colorbar(im, cax=ax3, orientation='horizontal',
278278
extend='both', values=values)
279-
plt.colorbar(im, cax=ax4, cmap=cmap, orientation='horizontal',
279+
plt.colorbar(im, cax=ax4, orientation='horizontal',
280280
extend='both', extendrect=True, values=values)
281-
plt.colorbar(im, cax=ax5, cmap=cmap, orientation='horizontal',
281+
plt.colorbar(im, cax=ax5, orientation='horizontal',
282282
extend='neither', values=values)
283283

284284

@@ -292,9 +292,8 @@ def test_colorbar_ticks():
292292
Z = X * Y
293293
clevs = np.array([-12, -5, 0, 5, 12], dtype=float)
294294
colors = ['r', 'g', 'b', 'c']
295-
cs = ax.contourf(X, Y, Z, clevs, colors=colors)
296-
cbar = fig.colorbar(cs, ax=ax, extend='neither',
297-
orientation='horizontal', ticks=clevs)
295+
cs = ax.contourf(X, Y, Z, clevs, colors=colors, extend='neither')
296+
cbar = fig.colorbar(cs, ax=ax, orientation='horizontal')
298297
assert len(cbar.ax.xaxis.get_ticklocs()) == len(clevs)
299298

300299

0 commit comments

Comments
 (0)