From 825c2424a2e8a4f1ff52050f1724f79db789822d Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 8 Oct 2018 14:36:38 +0200 Subject: [PATCH 1/2] Warn in colorbar() when mappable.axes != figure.gca(). --- doc/api/next_api_changes/deprecations/12443-AL.rst | 8 ++++++++ lib/matplotlib/blocking_input.py | 2 +- lib/matplotlib/figure.py | 12 ++++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 doc/api/next_api_changes/deprecations/12443-AL.rst diff --git a/doc/api/next_api_changes/deprecations/12443-AL.rst b/doc/api/next_api_changes/deprecations/12443-AL.rst new file mode 100644 index 000000000000..d080d6112052 --- /dev/null +++ b/doc/api/next_api_changes/deprecations/12443-AL.rst @@ -0,0 +1,8 @@ +Figure.colorbar now warns when the mappable's axes is different from the current axes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Currently, `.Figure.colorbar` steals space by default from the current axes +to place the colorbar. In a future version, it will steal space from the +mappable's axes instead. In preparation for this change, `.Figure.colorbar` +now emits a warning when the current axes is not the same as the mappable's +axes. diff --git a/lib/matplotlib/blocking_input.py b/lib/matplotlib/blocking_input.py index a1ec2849f41c..b9ce7981552e 100644 --- a/lib/matplotlib/blocking_input.py +++ b/lib/matplotlib/blocking_input.py @@ -277,7 +277,7 @@ class BlockingContourLabeler(BlockingMouseInput): def __init__(self, cs): self.cs = cs - BlockingMouseInput.__init__(self, fig=cs.ax.figure) + BlockingMouseInput.__init__(self, fig=cs.axes.figure) def add_click(self, event): self.button1(event) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 7c740ff39abf..32426c6ffec4 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -2318,13 +2318,21 @@ def colorbar(self, mappable, cax=None, ax=None, use_gridspec=True, **kw): """%(colorbar_doc)s""" if ax is None: ax = self.gca() + if (hasattr(mappable, "axes") and ax is not mappable.axes + and cax is None): + cbook.warn_deprecated( + "3.4", message="Starting from Matplotlib 3.6, colorbar() " + "will steal space from the mappable's axes, rather than " + "from the current axes, to place the colorbar. To " + "silence this warning, explicitly pass the 'ax' argument " + "to colorbar().") # Store the value of gca so that we can set it back later on. current_ax = self.gca() if cax is None: - if use_gridspec and isinstance(ax, SubplotBase) \ - and (not self.get_constrained_layout()): + if (use_gridspec and isinstance(ax, SubplotBase) + and not self.get_constrained_layout()): cax, kw = cbar.make_axes_gridspec(ax, **kw) else: cax, kw = cbar.make_axes(ax, **kw) From fa96f30d221cc4124194c1d8acebc8c5390bb9bb Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 6 Jul 2020 09:50:45 +0200 Subject: [PATCH 2/2] Also warn in pyplot.colorbar (by falling back to Figure.colorbar). --- doc/api/next_api_changes/deprecations/12443-AL.rst | 14 +++++++------- lib/matplotlib/pyplot.py | 2 -- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/doc/api/next_api_changes/deprecations/12443-AL.rst b/doc/api/next_api_changes/deprecations/12443-AL.rst index d080d6112052..cc1b6bac2c04 100644 --- a/doc/api/next_api_changes/deprecations/12443-AL.rst +++ b/doc/api/next_api_changes/deprecations/12443-AL.rst @@ -1,8 +1,8 @@ -Figure.colorbar now warns when the mappable's axes is different from the current axes -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +``colorbar`` now warns when the mappable's axes is different from the current axes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Currently, `.Figure.colorbar` steals space by default from the current axes -to place the colorbar. In a future version, it will steal space from the -mappable's axes instead. In preparation for this change, `.Figure.colorbar` -now emits a warning when the current axes is not the same as the mappable's -axes. +Currently, `.Figure.colorbar` and `.pyplot.colorbar` steal space by default +from the current axes to place the colorbar. In a future version, they will +steal space from the mappable's axes instead. In preparation for this change, +`.Figure.colorbar` and `.pyplot.colorbar` now emits a warning when the current +axes is not the same as the mappable's axes. diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 8d7b656106d1..064d2e511363 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -2181,8 +2181,6 @@ def colorbar(mappable=None, cax=None, ax=None, **kw): 'creation. First define a mappable such as ' 'an image (with imshow) or a contour set (' 'with contourf).') - if ax is None: - ax = gca() ret = gcf().colorbar(mappable, cax=cax, ax=ax, **kw) return ret