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

Skip to content

Commit 9c36d85

Browse files
committed
MNT: make fig.colorbar(..., ax=INPUT) even more forgiving
Accept anything that passes `np.iterable`
1 parent 011a3f7 commit 9c36d85

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

lib/matplotlib/colorbar.py

Lines changed: 8 additions & 5 deletions
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 of `~.axes.Axes`
13991398
The Axes to use as parents for placing the colorbar.
14001399
%(_make_axes_kw_doc)s
14011400
@@ -1419,10 +1418,14 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
14191418
# turn parents into a list if it is not already. Note we cannot
14201419
# use .flatten or .ravel as these copy the references rather than
14211420
# reuse them, leading to a memory leak
1422-
if isinstance(parents, np.ndarray):
1423-
parents = list(parents.flat)
1424-
elif not isinstance(parents, collections_abc.Sequence):
1421+
if np.iterable(parents):
1422+
if isinstance(parents, np.ndarray):
1423+
parents = list(parents.flat)
1424+
else:
1425+
parents = list(parents)
1426+
else:
14251427
parents = [parents]
1428+
14261429
fig = parents[0].get_figure()
14271430

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

lib/matplotlib/figure.py

Lines changed: 1 addition & 1 deletion
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 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

Lines changed: 1 addition & 0 deletions
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)