Skip to content

Commit 464072e

Browse files
committed
Warn in colorbar() when mappable.axes != figure.gca().
1 parent 9cffe0e commit 464072e

File tree

4 files changed

+49
-25
lines changed

4 files changed

+49
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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.
9+
10+
The :attr:`.ax` attribute to `ContourSet` is deprecated in favor of
11+
:attr:`.axes` for consistency with artists (even though `ContourSet` is not an
12+
`Artist` subclass).

lib/matplotlib/blocking_input.py

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

274274
def __init__(self, cs):
275275
self.cs = cs
276-
BlockingMouseInput.__init__(self, fig=cs.ax.figure)
276+
BlockingMouseInput.__init__(self, fig=cs.axes.figure)
277277

278278
def add_click(self, event):
279279
self.button1(event)

lib/matplotlib/contour.py

+27-22
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def set_label_props(self, label, text, color):
315315
label.set_text(text)
316316
label.set_color(color)
317317
label.set_fontproperties(self.labelFontProps)
318-
label.set_clip_box(self.ax.bbox)
318+
label.set_clip_box(self.axes.bbox)
319319

320320
def get_text(self, lev, fmt):
321321
"""Get the text of the label."""
@@ -457,7 +457,7 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
457457
return rotation, nlc
458458

459459
def _get_label_text(self, x, y, rotation):
460-
dx, dy = self.ax.transData.inverted().transform_point((x, y))
460+
dx, dy = self.axes.transData.inverted().transform_point((x, y))
461461
t = text.Text(dx, dy, rotation=rotation,
462462
horizontalalignment='center',
463463
verticalalignment='center')
@@ -468,7 +468,7 @@ def _get_label_clabeltext(self, x, y, rotation):
468468
# the data coordinate and create a label using ClabelText
469469
# class. This way, the rotation of the clabel is along the
470470
# contour line always.
471-
transDataInv = self.ax.transData.inverted()
471+
transDataInv = self.axes.transData.inverted()
472472
dx, dy = transDataInv.transform_point((x, y))
473473
drotation = transDataInv.transform_angles(np.array([rotation]),
474474
np.array([[x, y]]))
@@ -488,7 +488,7 @@ def _add_label(self, t, x, y, lev, cvalue):
488488
self.labelXYs.append((x, y))
489489

490490
# Add label to plot here - useful for manual mode label selection
491-
self.ax.add_artist(t)
491+
self.axes.add_artist(t)
492492

493493
def add_label(self, x, y, rotation, lev, cvalue):
494494
"""
@@ -534,7 +534,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
534534
"""
535535

536536
if transform is None:
537-
transform = self.ax.transData
537+
transform = self.axes.transData
538538

539539
if transform:
540540
x, y = transform.transform_point((x, y))
@@ -553,7 +553,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
553553
# grab its vertices
554554
lc = active_path.vertices
555555
# sort out where the new vertex should be added data-units
556-
xcmin = self.ax.transData.inverted().transform_point([xmin, ymin])
556+
xcmin = self.axes.transData.inverted().transform_point([xmin, ymin])
557557
# if there isn't a vertex close enough
558558
if not np.allclose(xcmin, lc[imin]):
559559
# insert new data into the vertex list
@@ -569,13 +569,13 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
569569
lc = paths[segmin].vertices
570570

571571
# In pixel/screen space
572-
slc = self.ax.transData.transform(lc)
572+
slc = self.axes.transData.transform(lc)
573573

574574
# Get label width for rotating labels and breaking contours
575575
lw = self.get_label_width(self.labelLevelList[lmin],
576576
self.labelFmt, self.labelFontSizeList[lmin])
577577
# lw is in points.
578-
lw *= self.ax.figure.dpi / 72.0 # scale to screen coordinates
578+
lw *= self.axes.figure.dpi / 72 # scale to screen coordinates
579579
# now lw in pixels
580580

581581
# Figure out label rotation.
@@ -619,7 +619,7 @@ def labels(self, inline, inline_spacing):
619619
con = self.collections[icon]
620620
trans = con.get_transform()
621621
lw = self.get_label_width(lev, self.labelFmt, fsize)
622-
lw *= self.ax.figure.dpi / 72.0 # scale to screen coordinates
622+
lw *= self.axes.figure.dpi / 72 # scale to screen coordinates
623623
additions = []
624624
paths = con.get_paths()
625625
for segNum, linepath in enumerate(paths):
@@ -840,7 +840,7 @@ def __init__(self, ax, *args,
840840
Keyword arguments are as described in the docstring of
841841
`~axes.Axes.contour`.
842842
"""
843-
self.ax = ax
843+
self.axes = ax
844844
self.levels = levels
845845
self.filled = filled
846846
self.linewidths = linewidths
@@ -959,7 +959,7 @@ def __init__(self, ax, *args,
959959
alpha=self.alpha,
960960
transform=self.get_transform(),
961961
zorder=zorder)
962-
self.ax.add_collection(col, autolim=False)
962+
self.axes.add_collection(col, autolim=False)
963963
self.collections.append(col)
964964
else:
965965
tlinewidths = self._process_linewidths()
@@ -981,14 +981,14 @@ def __init__(self, ax, *args,
981981
transform=self.get_transform(),
982982
zorder=zorder)
983983
col.set_label('_nolegend_')
984-
self.ax.add_collection(col, autolim=False)
984+
self.axes.add_collection(col, autolim=False)
985985
self.collections.append(col)
986986

987987
for col in self.collections:
988988
col.sticky_edges.x[:] = [self._mins[0], self._maxs[0]]
989989
col.sticky_edges.y[:] = [self._mins[1], self._maxs[1]]
990-
self.ax.update_datalim([self._mins, self._maxs])
991-
self.ax.autoscale_view(tight=True)
990+
self.axes.update_datalim([self._mins, self._maxs])
991+
self.axes.autoscale_view(tight=True)
992992

993993
self.changed() # set the colors
994994

@@ -997,16 +997,21 @@ def __init__(self, ax, *args,
997997
warnings.warn('The following kwargs were not used by contour: ' +
998998
s)
999999

1000+
@cbook.deprecated("3.1")
1001+
@property
1002+
def ax(self):
1003+
return self.axes
1004+
10001005
def get_transform(self):
10011006
"""
10021007
Return the :class:`~matplotlib.transforms.Transform`
10031008
instance used by this ContourSet.
10041009
"""
10051010
if self._transform is None:
1006-
self._transform = self.ax.transData
1011+
self._transform = self.axes.transData
10071012
elif (not isinstance(self._transform, mtransforms.Transform)
10081013
and hasattr(self._transform, '_as_mpl_transform')):
1009-
self._transform = self._transform._as_mpl_transform(self.ax)
1014+
self._transform = self._transform._as_mpl_transform(self.axes)
10101015
return self._transform
10111016

10121017
def __getstate__(self):
@@ -1497,9 +1502,9 @@ def _process_args(self, *args, **kwargs):
14971502

14981503
# if the transform is not trans data, and some part of it
14991504
# contains transData, transform the xs and ys to data coordinates
1500-
if (t != self.ax.transData and
1501-
any(t.contains_branch_seperately(self.ax.transData))):
1502-
trans_to_data = t - self.ax.transData
1505+
if (t != self.axes.transData and
1506+
any(t.contains_branch_seperately(self.axes.transData))):
1507+
trans_to_data = t - self.axes.transData
15031508
pts = (np.vstack([x.flat, y.flat]).T)
15041509
transformed_pts = trans_to_data.transform(pts)
15051510
x = transformed_pts[..., 0]
@@ -1567,9 +1572,9 @@ def _check_xyz(self, args, kwargs):
15671572
Exception class (here and elsewhere).
15681573
"""
15691574
x, y = args[:2]
1570-
kwargs = self.ax._process_unit_info(xdata=x, ydata=y, kwargs=kwargs)
1571-
x = self.ax.convert_xunits(x)
1572-
y = self.ax.convert_yunits(y)
1575+
kwargs = self.axes._process_unit_info(xdata=x, ydata=y, kwargs=kwargs)
1576+
x = self.axes.convert_xunits(x)
1577+
y = self.axes.convert_yunits(y)
15731578

15741579
x = np.asarray(x, dtype=np.float64)
15751580
y = np.asarray(y, dtype=np.float64)

lib/matplotlib/figure.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -2127,13 +2127,20 @@ def colorbar(self, mappable, cax=None, ax=None, use_gridspec=True, **kw):
21272127
"""
21282128
if ax is None:
21292129
ax = self.gca()
2130+
if ax is not mappable.axes:
2131+
cbook.warn_deprecated(
2132+
"3.1", message="Starting from Matplotlib 3.3, colorbar() "
2133+
"will steal space from the mappable's axes, rather than "
2134+
"from the current axes, to place the colorbar. To "
2135+
"silence this warning, explicitly pass the 'ax' argument "
2136+
"to colorbar().")
21302137

21312138
# Store the value of gca so that we can set it back later on.
21322139
current_ax = self.gca()
21332140

21342141
if cax is None:
2135-
if use_gridspec and isinstance(ax, SubplotBase) \
2136-
and (not self.get_constrained_layout()):
2142+
if (use_gridspec and isinstance(ax, SubplotBase)
2143+
and not self.get_constrained_layout()):
21372144
cax, kw = cbar.make_axes_gridspec(ax, **kw)
21382145
else:
21392146
cax, kw = cbar.make_axes(ax, **kw)

0 commit comments

Comments
 (0)