Skip to content

Commit 825c242

Browse files
committed
Warn in colorbar() when mappable.axes != figure.gca().
1 parent 8e7c7ab commit 825c242

File tree

3 files changed

+19
-3
lines changed

3 files changed

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

lib/matplotlib/blocking_input.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,13 +2318,21 @@ def colorbar(self, mappable, cax=None, ax=None, use_gridspec=True, **kw):
23182318
"""%(colorbar_doc)s"""
23192319
if ax is None:
23202320
ax = self.gca()
2321+
if (hasattr(mappable, "axes") and ax is not mappable.axes
2322+
and cax is None):
2323+
cbook.warn_deprecated(
2324+
"3.4", message="Starting from Matplotlib 3.6, colorbar() "
2325+
"will steal space from the mappable's axes, rather than "
2326+
"from the current axes, to place the colorbar. To "
2327+
"silence this warning, explicitly pass the 'ax' argument "
2328+
"to colorbar().")
23212329

23222330
# Store the value of gca so that we can set it back later on.
23232331
current_ax = self.gca()
23242332

23252333
if cax is None:
2326-
if use_gridspec and isinstance(ax, SubplotBase) \
2327-
and (not self.get_constrained_layout()):
2334+
if (use_gridspec and isinstance(ax, SubplotBase)
2335+
and not self.get_constrained_layout()):
23282336
cax, kw = cbar.make_axes_gridspec(ax, **kw)
23292337
else:
23302338
cax, kw = cbar.make_axes(ax, **kw)

0 commit comments

Comments
 (0)