Skip to content

Commit 98961f6

Browse files
authored
Merge pull request #12443 from anntzer/colorbar-mappable-axes
Warn in colorbar() when mappable.axes != figure.gca().
2 parents bb9d11d + fa96f30 commit 98961f6

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
``colorbar`` now warns when the mappable's axes is different from the current axes
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Currently, `.Figure.colorbar` and `.pyplot.colorbar` steal space by default
5+
from the current axes to place the colorbar. In a future version, they will
6+
steal space from the mappable's axes instead. In preparation for this change,
7+
`.Figure.colorbar` and `.pyplot.colorbar` now emits a warning when the current
8+
axes is not the same as the mappable's axes.

lib/matplotlib/blocking_input.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class BlockingContourLabeler(BlockingMouseInput):
277277

278278
def __init__(self, cs):
279279
self.cs = cs
280-
BlockingMouseInput.__init__(self, fig=cs.ax.figure)
280+
BlockingMouseInput.__init__(self, fig=cs.axes.figure)
281281

282282
def add_click(self, event):
283283
self.button1(event)

lib/matplotlib/figure.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -2322,13 +2322,21 @@ def colorbar(self, mappable, cax=None, ax=None, use_gridspec=True, **kw):
23222322
"""%(colorbar_doc)s"""
23232323
if ax is None:
23242324
ax = self.gca()
2325+
if (hasattr(mappable, "axes") and ax is not mappable.axes
2326+
and cax is None):
2327+
cbook.warn_deprecated(
2328+
"3.4", message="Starting from Matplotlib 3.6, colorbar() "
2329+
"will steal space from the mappable's axes, rather than "
2330+
"from the current axes, to place the colorbar. To "
2331+
"silence this warning, explicitly pass the 'ax' argument "
2332+
"to colorbar().")
23252333

23262334
# Store the value of gca so that we can set it back later on.
23272335
current_ax = self.gca()
23282336

23292337
if cax is None:
2330-
if use_gridspec and isinstance(ax, SubplotBase) \
2331-
and (not self.get_constrained_layout()):
2338+
if (use_gridspec and isinstance(ax, SubplotBase)
2339+
and not self.get_constrained_layout()):
23322340
cax, kw = cbar.make_axes_gridspec(ax, **kw)
23332341
else:
23342342
cax, kw = cbar.make_axes(ax, **kw)

lib/matplotlib/pyplot.py

-2
Original file line numberDiff line numberDiff line change
@@ -2167,8 +2167,6 @@ def colorbar(mappable=None, cax=None, ax=None, **kw):
21672167
'creation. First define a mappable such as '
21682168
'an image (with imshow) or a contour set ('
21692169
'with contourf).')
2170-
if ax is None:
2171-
ax = gca()
21722170
ret = gcf().colorbar(mappable, cax=cax, ax=ax, **kw)
21732171
return ret
21742172

0 commit comments

Comments
 (0)