Skip to content

ENH: add colorbar method to axes #12333

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
wants to merge 1 commit into from
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
8 changes: 8 additions & 0 deletions doc/users/next_whats_new/2018-09-28-JMK.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:orphan:

New `.axes.Axes.colorbar` method for attaching colorbar to single axes
``````````````````````````````````````````````````````````````````````

A simple wrapper for `.Figure.colorbar` that allows the user to omit the
``ax`` kwarg. The new ``ax.colorbar(mappable)`` is equivalent to
``fig.colorbar(mappable, ax=ax)``.
15 changes: 15 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,21 @@ def legend(self, *args, **kwargs):
def _remove_legend(self, legend):
self.legend_ = None

@docstring.dedent_interpd
def colorbar(self, mappable, cax=None, use_gridspec=True, **kw):
"""
Create a colorbar for a ScalarMappable instance, *mappable*, with the
axes as the parent.

Documentation for the pyplot thin wrapper:
%(colorbar_doc)s
"""
ax = kw.pop('ax', None)
if ax is not None:
warnings.warn('Supplied ax argument ignored')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just make this an error, not much point of being tolerant in a new API...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say it pretty much depends on how the documentation is written. If it says "allowed kwargs are all matplotlib.figure.Figure.colorbar arguments", then it should warn. If it explicitely says "allowed kwargs are all matplotlib.figure.Figure.colorbar arguments, except ax", it can error out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 to raising. In someplaces we have to tolerate ignoring input because it is old API, but we should not add ignored kwargs in new API.

self.figure.colorbar(mappable, ax=self, cax=cax,
use_gridspec=use_gridspec, **kw)

def inset_axes(self, bounds, *, transform=None, zorder=5,
**kwargs):
"""
Expand Down
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,19 @@ def test_colorbar_axes_kw():
plt.imshow(([[1, 2], [3, 4]]))
plt.colorbar(orientation='horizontal', fraction=0.2, pad=0.2, shrink=0.5,
aspect=10, anchor=(0., 0.), panchor=(0., 1.))


def test_colorbar_axes():
fig, ax = plt.subplots()
pcm = ax.pcolormesh(np.random.random((32, 32)))
# smoketest that this works:
ax.colorbar(pcm)
with pytest.warns(UserWarning) as record:
ax.colorbar(pcm, ax=ax)
assert len(record) == 1

fig, ax = plt.subplots()
pcm = ax.pcolormesh(np.random.random((32, 32)))
# smoketest that this works:
ax.colorbar(pcm, orientation='horizontal', fraction=0.2, pad=0.2, shrink=0.5,
aspect=10, anchor=(0., 0.), panchor=(0., 1.))
5 changes: 3 additions & 2 deletions lib/mpl_toolkits/axes_grid1/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
the :class:`~matplotlib.image.Image`,
:class:`~matplotlib.contour.ContourSet`, etc. to
which the colorbar applies; this argument is mandatory for the
:meth:`~matplotlib.figure.Figure.colorbar` method but optional for the
:meth:`~matplotlib.figure.Figure.colorbar`
and :meth:`~matplotlib.axes.Axes.colorbar` methods but optional for the
:func:`~matplotlib.pyplot.colorbar` function, which sets the
default to the current image.

Expand All @@ -119,7 +120,7 @@
None | axes object into which the colorbar will be drawn
*ax*
None | parent axes object from which space for a new
colorbar axes will be stolen
colorbar axes will be stolen (ignored for `.axes.Axes.colorbar`)


Additional keyword arguments are of two kinds:
Expand Down