Skip to content

MNT: make fig.colorbar(..., ax=INPUT) even more forgiving #24737

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

Merged
merged 3 commits into from
Dec 16, 2022
Merged
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: 5 additions & 3 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
End-users most likely won't need to directly use this module's API.
"""

import collections.abc as collections_abc
import logging

import numpy as np
Expand Down Expand Up @@ -1395,7 +1394,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,

Parameters
----------
parents : `~.axes.Axes` or sequence or `numpy.ndarray` of `~.axes.Axes`
parents : `~.axes.Axes` or iterable or `numpy.ndarray` of `~.axes.Axes`
The Axes to use as parents for placing the colorbar.
%(_make_axes_kw_doc)s

Expand All @@ -1421,8 +1420,11 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
# reuse them, leading to a memory leak
if isinstance(parents, np.ndarray):
parents = list(parents.flat)
elif not isinstance(parents, collections_abc.Sequence):
elif np.iterable(parents):
parents = list(parents)
else:
parents = [parents]

fig = parents[0].get_figure()

pad0 = 0.05 if fig.get_constrained_layout() else loc_settings['pad']
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ def colorbar(
cax : `~matplotlib.axes.Axes`, optional
Axes into which the colorbar will be drawn.

ax : `~.axes.Axes` or sequence or `numpy.ndarray` of Axes, optional
ax : `~.axes.Axes` or iterable or `numpy.ndarray` of Axes, optional
One or more parent axes from which space for a new colorbar axes
will be stolen, if *cax* is None. This has no effect if *cax* is
set.
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,4 +1209,5 @@ def test_colorbar_axes_parmeters():
fig.colorbar(im, ax=ax[0])
fig.colorbar(im, ax=[_ax for _ax in ax])
fig.colorbar(im, ax=(ax[0], ax[1]))
fig.colorbar(im, ax={i: _ax for i, _ax in enumerate(ax)}.values())
fig.draw_without_rendering()