8000 MNT: more control of colorbar with CountourSet · matplotlib/matplotlib@6127cc6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6127cc6

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 7131970 commit 6127cc6

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed
Lines changed: 12 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
CS = plt.contour(Z, levels,
8282
origin='lower',
8383
linewidths=2,
84-
extent=(-3, 3, -2, 2))
84+
extent=(-3, 3, -2, 2), extend='both')
8585

8686
# Thicken the zero contour.
8787
zc = CS.collections[6]
@@ -93,7 +93,7 @@
9393
fontsize=14)
9494

9595
# make a colorbar for the contour lines
96-
CB = plt.colorbar(CS, shrink=0.8, extend='both')
96+
CB = plt.colorbar(CS, shrink=0.8)
9797

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

lib/matplotlib/colorbar.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,18 +880,21 @@ def __init__(self, ax, mappable, **kw):
880880
mappable.autoscale_None()
881881

882882
self.mappable = mappable
883+
kw = cbook.normalize_kwargs(kw, forbidden=('cmap', 'norm'))
883884
kw['cmap'] = cmap = mappable.cmap
884-
kw['norm'] = norm = mappable.norm
885+
kw['norm'] = mappable.norm
885886

886887
if isinstance(mappable, contour.ContourSet):
887888
CS = mappable
889+
override_list = ('alpha', 'boundaries', 'values', 'extend',
890+
'filled')
891+
kw = cbook.normalize_kwargs(kw, forbidden=override_list)
888892
kw['alpha'] = mappable.get_alpha()
889893
kw['boundaries'] = CS._levels
890894
kw['values'] = CS.cvalues
891895
kw['extend'] = CS.extend
892-
#kw['ticks'] = CS._levels
893-
kw.setdefault('ticks', ticker.FixedLocator(CS.levels, nbins=10))
894896
kw['filled'] = CS.filled
897+
kw.setdefault('ticks', ticker.FixedLocator(CS.levels, nbins=10))
895898
ColorbarBase.__init__(self, ax, **kw)
896899
if not CS.filled:
897900
self.add_lines(CS)
@@ -900,7 +903,7 @@ def __init__(self, ax, mappable, **kw):
900903
kw.setdefault('extend', cmap.colorbar_extend)
901904

902905
if isinstance(mappable, martist.Artist):
903-
kw['alpha'] = mappable.get_alpha()
906+
kw.setdefault('alpha', mappable.get_alpha())
904907

905908
ColorbarBase.__init__(self, ax, **kw)
906909

lib/matplotlib/tests/test_colorbar.py

Lines changed: 6 additions & 7 deletions
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)
0