Skip to content

Warn in colorbar() when mappable.axes != figure.gca(). #12443

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 2 commits into from
Jul 18, 2020
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: 8 additions & 0 deletions doc/api/next_api_changes/deprecations/12443-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
``colorbar`` now warns when the mappable's axes is different from the current 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.
2 changes: 1 addition & 1 deletion lib/matplotlib/blocking_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down