Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0084101

Browse files
committed
ENH: add colorbar method to axes
1 parent c574969 commit 0084101

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:orphan:
2+
3+
New `.axes.Axes.colorbar` method for attaching colorbar to single axes
4+
``````````````````````````````````````````````````````````````````````
5+
6+
A simple wrapper for `.Figure.colorbar` that allows the user to omit the
7+
``ax`` kwarg. The new ``ax.colorbar(mappable)`` is equivalent to
8+
``fig.colorbar(mappable, ax=ax)``.

lib/matplotlib/axes/_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,21 @@ def legend(self, *args, **kwargs):
418418
def _remove_legend(self, legend):
419419
self.legend_ = None
420420

421+
@docstring.dedent_interpd
422+
def colorbar(self, mappable, cax=None, use_gridspec=True, **kw):
423+
"""
424+
Create a colorbar for a ScalarMappable instance, *mappable*, with the
425+
axes as the parent.
426+
427+
Documentation for the pyplot thin wrapper:
428+
%(colorbar_doc)s
429+
"""
430+
ax = kw.pop('ax', None)
431+
if ax is not None:
432+
warnings.warn('Supplied ax argument ignored')
433+
self.figure.colorbar(mappable, ax=self, cax=cax,
434+
use_gridspec=use_gridspec, **kw)
435+
421436
def inset_axes(self, bounds, *, transform=None, zorder=5,
422437
**kwargs):
423438
"""

lib/matplotlib/tests/test_colorbar.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,19 @@ def test_colorbar_axes_kw():
396396
plt.imshow(([[1, 2], [3, 4]]))
397397
plt.colorbar(orientation='horizontal', fraction=0.2, pad=0.2, shrink=0.5,
398398
aspect=10, anchor=(0., 0.), panchor=(0., 1.))
399+
400+
401+
def test_colorbar_axes():
402+
fig, ax = plt.subplots()
403+
pcm = ax.pcolormesh(np.random.random((32, 32)))
404+
# smoketest that this works:
405+
ax.colorbar(pcm)
406+
with pytest.warns(UserWarning) as record:
407+
ax.colorbar(pcm, ax=ax)
408+
assert len(record) == 1
409+
410+
fig, ax = plt.subplots()
411+
pcm = ax.pcolormesh(np.random.random((32, 32)))
412+
# smoketest that this works:
413+
ax.colorbar(pcm, orientation='horizontal', fraction=0.2, pad=0.2, shrink=0.5,
414+
aspect=10, anchor=(0., 0.), panchor=(0., 1.))

lib/mpl_toolkits/axes_grid1/colorbar.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@
109109
the :class:`~matplotlib.image.Image`,
110110
:class:`~matplotlib.contour.ContourSet`, etc. to
111111
which the colorbar applies; this argument is mandatory for the
112-
:meth:`~matplotlib.figure.Figure.colorbar` method but optional for the
112+
:meth:`~matplotlib.figure.Figure.colorbar`
113+
and :meth:`~matplotlib.axes.Axes.colorbar` methods but optional for the
113114
:func:`~matplotlib.pyplot.colorbar` function, which sets the
114115
default to the current image.
115116
@@ -119,7 +120,7 @@
119120
None | axes object into which the colorbar will be drawn
120121
*ax*
121122
None | parent axes object from which space for a new
122-
colorbar axes will be stolen
123+
colorbar axes will be stolen (ignored for `.axes.Axes.colorbar`)
123124
124125
125126
Additional keyword arguments are of two kinds:

0 commit comments

Comments
 (0)