From 9c36d85feaa81254e63f6575f81a90ba71207088 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 15 Dec 2022 12:21:08 -0500 Subject: [PATCH 1/3] MNT: make fig.colorbar(..., ax=INPUT) even more forgiving Accept anything that passes `np.iterable` --- lib/matplotlib/colorbar.py | 13 ++++++++----- lib/matplotlib/figure.py | 2 +- lib/matplotlib/tests/test_colorbar.py | 1 + 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 1272ac6ff3d8..4d2db83f89e3 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -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 @@ -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 of `~.axes.Axes` The Axes to use as parents for placing the colorbar. %(_make_axes_kw_doc)s @@ -1419,10 +1418,14 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15, # turn parents into a list if it is not already. Note we cannot # use .flatten or .ravel as these copy the references rather than # reuse them, leading to a memory leak - if isinstance(parents, np.ndarray): - parents = list(parents.flat) - elif not isinstance(parents, collections_abc.Sequence): + if np.iterable(parents): + if isinstance(parents, np.ndarray): + parents = list(parents.flat) + else: + parents = list(parents) + else: parents = [parents] + fig = parents[0].get_figure() pad0 = 0.05 if fig.get_constrained_layout() else loc_settings['pad'] diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 8ef03f9b2953..753c074d35f6 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -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 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. diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 981da4ab2f3b..51208756fb17 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -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() From bb510b9f64774c894040f9e6d8b9e1ac4bf55f19 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 15 Dec 2022 15:00:51 -0500 Subject: [PATCH 2/3] MNT: refactor to remove a level of indentation Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com> --- lib/matplotlib/colorbar.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 4d2db83f89e3..c3d977ca9750 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1418,11 +1418,10 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15, # turn parents into a list if it is not already. Note we cannot # use .flatten or .ravel as these copy the references rather than # reuse them, leading to a memory leak - if np.iterable(parents): - if isinstance(parents, np.ndarray): - parents = list(parents.flat) - else: - parents = list(parents) + if isinstance(parents, np.ndarray): + parents = list(parents.flat) + elif np.iterable(parents): + parents = list(parents) else: parents = [parents] From 06396e25677a7fc996a3d8dfd18ad8f7965808d7 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 15 Dec 2022 16:59:35 -0500 Subject: [PATCH 3/3] DOC: call out numpy array specifically We support higher-dimensional arrays (not clear we _should_ but we do). --- lib/matplotlib/colorbar.py | 2 +- lib/matplotlib/figure.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index c3d977ca9750..c73cff36dd69 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1394,7 +1394,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15, Parameters ---------- - parents : `~.axes.Axes` or iterable 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 diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 753c074d35f6..658201672e40 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1195,7 +1195,7 @@ def colorbar( cax : `~matplotlib.axes.Axes`, optional Axes into which the colorbar will be drawn. - ax : `~.axes.Axes` or iterable 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.