Skip to content

MNT: more control of colorbar with CountourSet #5056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/api/api_changes/2016-01-03_colorbar.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
`ColorBar` raises TypeError rather than ignore user input
`````````````````````````````````````````````````````````

In ``__init__`` `ColorBar` ignores user supplied ``cmap`` and ``norm``
keyword arguments in favor of retrieving those values from the
``mappable`` passed in. If those keywords are supplied, `ColorBar`
will now raise a `TypeError` rather than silently dropping user supplied
input.

If ``mappable`` is a `contour.ContourSet` then a `TypeError` will be raised
if the user provides any of
``('alpha', 'boundaries', 'values', 'extend', 'filled')``
4 changes: 2 additions & 2 deletions examples/pylab_examples/contour_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
CS = plt.contour(Z, levels,
origin='lower',
linewidths=2,
extent=(-3, 3, -2, 2))
extent=(-3, 3, -2, 2), extend='both')

# Thicken the zero contour.
zc = CS.collections[6]
Expand All @@ -92,7 +92,7 @@
fontsize=14)

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

plt.title('Lines with colorbar')
#plt.hot() # Now change the colormap for the contour lines and colorbar
Expand Down
11 changes: 7 additions & 4 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,18 +889,21 @@ def __init__(self, ax, mappable, **kw):
mappable.autoscale_None()

self.mappable = mappable
kw = cbook.normalize_kwargs(kw, forbidden=('cmap', 'norm'))
kw['cmap'] = cmap = mappable.cmap
kw['norm'] = norm = mappable.norm
kw['norm'] = mappable.norm

if isinstance(mappable, contour.ContourSet):
CS = mappable
override_list = ('alpha', 'boundaries', 'values', 'extend',
'filled')
kw = cbook.normalize_kwargs(kw, forbidden=override_list)
kw['alpha'] = mappable.get_alpha()
kw['boundaries'] = CS._levels
kw['values'] = CS.cvalues
kw['extend'] = CS.extend
#kw['ticks'] = CS._levels
kw.setdefault('ticks', ticker.FixedLocator(CS.levels, nbins=10))
kw['filled'] = CS.filled
kw.setdefault('ticks', ticker.FixedLocator(CS.levels, nbins=10))
ColorbarBase.__init__(self, ax, **kw)
if not CS.filled:
self.add_lines(CS)
Expand All @@ -909,7 +912,7 @@ def __init__(self, ax, mappable, **kw):
kw.setdefault('extend', cmap.colorbar_extend)

if isinstance(mappable, martist.Artist):
kw['alpha'] = mappable.get_alpha()
kw.setdefault('alpha', mappable.get_alpha())

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

Expand Down
13 changes: 6 additions & 7 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ def test_colorbar_closed_patch():
values = np.linspace(0, 10, 5)

with rc_context({'axes.linewidth': 16}):
plt.colorbar(im, cax=ax2, cmap=cmap, orientation='horizontal',
plt.colorbar(im, cax=ax2, orientation='horizontal',
extend='both', extendfrac=0.5, values=values)
plt.colorbar(im, cax=ax3, cmap=cmap, orientation='horizontal',
plt.colorbar(im, cax=ax3, orientation='horizontal',
extend='both', values=values)
plt.colorbar(im, cax=ax4, cmap=cmap, orientation='horizontal',
plt.colorbar(im, cax=ax4, orientation='horizontal',
extend='both', extendrect=True, values=values)
plt.colorbar(im, cax=ax5, cmap=cmap, orientation='horizontal',
plt.colorbar(im, cax=ax5, orientation='horizontal',
extend='neither', values=values)


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


Expand Down