Skip to content

Commit 7f5335a

Browse files
authored
Merge pull request #24737 from tacaswell/mnt/forgiving_colorbar_input
MNT: make fig.colorbar(..., ax=INPUT) even more forgiving
2 parents 12402f4 + 06396e2 commit 7f5335a

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

lib/matplotlib/colorbar.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
End-users most likely won't need to directly use this module's API.
1212
"""
1313

14-
import collections.abc as collections_abc
1514
import logging
1615

1716
import numpy as np
@@ -1395,7 +1394,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
13951394
13961395
Parameters
13971396
----------
1398-
parents : `~.axes.Axes` or sequence or `numpy.ndarray` of `~.axes.Axes`
1397+
parents : `~.axes.Axes` or iterable or `numpy.ndarray` of `~.axes.Axes`
13991398
The Axes to use as parents for placing the colorbar.
14001399
%(_make_axes_kw_doc)s
14011400
@@ -1421,8 +1420,11 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
14211420
# reuse them, leading to a memory leak
14221421
if isinstance(parents, np.ndarray):
14231422
parents = list(parents.flat)
1424-
elif not isinstance(parents, collections_abc.Sequence):
1423+
elif np.iterable(parents):
1424+
parents = list(parents)
1425+
else:
14251426
parents = [parents]
1427+
14261428
fig = parents[0].get_figure()
14271429

14281430
pad0 = 0.05 if fig.get_constrained_layout() else loc_settings['pad']

lib/matplotlib/figure.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ def colorbar(
11951195
cax : `~matplotlib.axes.Axes`, optional
11961196
Axes into which the colorbar will be drawn.
11971197
1198-
ax : `~.axes.Axes` or sequence or `numpy.ndarray` of Axes, optional
1198+
ax : `~.axes.Axes` or iterable or `numpy.ndarray` of Axes, optional
11991199
One or more parent axes from which space for a new colorbar axes
12001200
will be stolen, if *cax* is None. This has no effect if *cax* is
12011201
set.

lib/matplotlib/tests/test_colorbar.py

+1
Original file line numberDiff line numberDiff line change
@@ -1209,4 +1209,5 @@ def test_colorbar_axes_parmeters():
12091209
fig.colorbar(im, ax=ax[0])
12101210
fig.colorbar(im, ax=[_ax for _ax in ax])
12111211
fig.colorbar(im, ax=(ax[0], ax[1]))
1212+
fig.colorbar(im, ax={i: _ax for i, _ax in enumerate(ax)}.values())
12121213
fig.draw_without_rendering()

0 commit comments

Comments
 (0)