Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb5112d

Browse files
committed
Warn in colorbar() when mappable.axes != figure.gca().
1 parent 64ba969 commit fb5112d

File tree

4 files changed

+49
-25
lines changed

4 files changed

+49
-25
lines changed
Lines changed: 12 additions & 0 deletions
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

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

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def set_label_props(self, label, text, color):
260260
label.set_text(text)
261261
label.set_color(color)
262262
label.set_fontproperties(self.labelFontProps)
263-
label.set_clip_box(self.ax.bbox)
263+
label.set_clip_box(self.axes.bbox)
264264

265265
def get_text(self, lev, fmt):
266266
"""Get the text of the label."""
@@ -402,7 +402,7 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
402402
return rotation, nlc
403403

404404
def _get_label_text(self, x, y, rotation):
405-
dx, dy = self.ax.transData.inverted().transform((x, y))
405+
dx, dy = self.axes.transData.inverted().transform((x, y))
406406
t = text.Text(dx, dy, rotation=rotation,
407407
horizontalalignment='center',
408408
verticalalignment='center')
@@ -413,7 +413,7 @@ def _get_label_clabeltext(self, x, y, rotation):
413413
# the data coordinate and create a label using ClabelText
414414
# class. This way, the rotation of the clabel is along the
415415
# contour line always.
416-
transDataInv = self.ax.transData.inverted()
416+
transDataInv = self.axes.transData.inverted()
417417
dx, dy = transDataInv.transform((x, y))
418418
drotation = transDataInv.transform_angles(np.array([rotation]),
419419
np.array([[x, y]]))
@@ -433,7 +433,7 @@ def _add_label(self, t, x, y, lev, cvalue):
433433
self.labelXYs.append((x, y))
434434

435435
# Add label to plot here - useful for manual mode label selection
436-
self.ax.add_artist(t)
436+
self.axes.add_artist(t)
437437

438438
def add_label(self, x, y, rotation, lev, cvalue):
439439
"""
@@ -477,7 +477,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
477477
"""
478478

479479
if transform is None:
480-
transform = self.ax.transData
480+
transform = self.axes.transData
481481

482482
if transform:
483483
x, y = transform.transform((x, y))
@@ -496,7 +496,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
496496
# grab its vertices
497497
lc = active_path.vertices
498498
# sort out where the new vertex should be added data-units
499-
xcmin = self.ax.transData.inverted().transform([xmin, ymin])
499+
xcmin = self.axes.transData.inverted().transform([xmin, ymin])
500500
# if there isn't a vertex close enough
501501
if not np.allclose(xcmin, lc[imin]):
502502
# insert new data into the vertex list
@@ -512,13 +512,13 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
512512
lc = paths[segmin].vertices
513513

514514
# In pixel/screen space
515-
slc = self.ax.transData.transform(lc)
515+
slc = self.axes.transData.transform(lc)
516516

517517
# Get label width for rotating labels and breaking contours
518518
lw = self.get_label_width(self.labelLevelList[lmin],
519519
self.labelFmt, self.labelFontSizeList[lmin])
520520
# lw is in points.
521-
lw *= self.ax.figure.dpi / 72.0 # scale to screen coordinates
521+
lw *= self.axes.figure.dpi / 72 # scale to screen coordinates
522522
# now lw in pixels
523523

524524
# Figure out label rotation.
@@ -562,7 +562,7 @@ def labels(self, inline, inline_spacing):
562562
con = self.collections[icon]
563563
trans = con.get_transform()
564564
lw = self.get_label_width(lev, self.labelFmt, fsize)
565-
lw *= self.ax.figure.dpi / 72.0 # scale to screen coordinates
565+
lw *= self.axes.figure.dpi / 72 # scale to screen coordinates
566566
additions = []
567567
paths = con.get_paths()
568568
for segNum, linepath in enumerate(paths):
@@ -784,7 +784,7 @@ def __init__(self, ax, *args,
784784
Keyword arguments are as described in the docstring of
785785
`~axes.Axes.contour`.
786786
"""
787-
self.ax = ax
787+
self.axes = ax
788788
self.levels = levels
789789
self.filled = filled
790790
self.linewidths = linewidths
@@ -900,7 +900,7 @@ def __init__(self, ax, *args,
900900
alpha=self.alpha,
901901
transform=self.get_transform(),
902902
zorder=zorder)
903-
self.ax.add_collection(col, autolim=False)
903+
self.axes.add_collection(col, autolim=False)
904904
self.collections.append(col)
905905
else:
906906
tlinewidths = self._process_linewidths()
@@ -922,14 +922,14 @@ def __init__(self, ax, *args,
922922
transform=self.get_transform(),
923923
zorder=zorder)
924924
col.set_label('_nolegend_')
925-
self.ax.add_collection(col, autolim=False)
925+
self.axes.add_collection(col, autolim=False)
926926
self.collections.append(col)
927927

928928
for col in self.collections:
929929
col.sticky_edges.x[:] = [self._mins[0], self._maxs[0]]
930930
col.sticky_edges.y[:] = [self._mins[1], self._maxs[1]]
931-
self.ax.update_datalim([self._mins, self._maxs])
932-
self.ax.autoscale_view(tight=True)
931+
self.axes.update_datalim([self._mins, self._maxs])
932+
self.axes.autoscale_view(tight=True)
933933

934934
self.changed() # set the colors
935935

@@ -938,16 +938,21 @@ def __init__(self, ax, *args,
938938
cbook._warn_external('The following kwargs were not used by '
939939
'contour: ' + s)
940940

941+
@cbook.deprecated("3.1")
942+
@property
943+
def ax(self):
944+
return self.axes
945+
941946
def get_transform(self):
942947
"""
943948
Return the :class:`~matplotlib.transforms.Transform`
944949
instance used by this ContourSet.
945950
"""
946951
if self._transform is None:
947-
self._transform = self.ax.transData
952+
self._transform = self.axes.transData
948953
elif (not isinstance(self._transform, mtransforms.Transform)
949954
and hasattr(self._transform, '_as_mpl_transform')):
950-
self._transform = self._transform._as_mpl_transform(self.ax)
955+
self._transform = self._transform._as_mpl_transform(self.axes)
951956
return self._transform
952957

953958
def __getstate__(self):
@@ -1433,9 +1438,9 @@ def _process_args(self, *args, **kwargs):
14331438

14341439
# if the transform is not trans data, and some part of it
14351440
# contains transData, transform the xs and ys to data coordinates
1436-
if (t != self.ax.transData and
1437-
any(t.contains_branch_seperately(self.ax.transData))):
1438-
trans_to_data = t - self.ax.transData
1441+
if (t != self.axes.transData and
1442+
any(t.contains_branch_seperately(self.axes.transData))):
1443+
trans_to_data = t - self.axes.transData
14391444
pts = (np.vstack([x.flat, y.flat]).T)
14401445
transformed_pts = trans_to_data.transform(pts)
14411446
x = transformed_pts[..., 0]
@@ -1504,9 +1509,9 @@ def _check_xyz(self, args, kwargs):
15041509
Exception class (here and elsewhere).
15051510
"""
15061511
x, y = args[:2]
1507-
kwargs = self.ax._process_unit_info(xdata=x, ydata=y, kwargs=kwargs)
1508-
x = self.ax.convert_xunits(x)
1509-
y = self.ax.convert_yunits(y)
1512+
kwargs = self.axes._process_unit_info(xdata=x, ydata=y, kwargs=kwargs)
1513+
x = self.axes.convert_xunits(x)
1514+
y = self.axes.convert_yunits(y)
15101515

15111516
x = np.asarray(x, dtype=np.float64)
15121517
y = np.asarray(y, dtype=np.float64)

lib/matplotlib/figure.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,13 +2207,20 @@ def colorbar(self, mappable, cax=None, ax=None, use_gridspec=True, **kw):
22072207
"""
22082208
if ax is None:
22092209
ax = self.gca()
2210+
if ax is not mappable.axes:
2211+
cbook.warn_deprecated(
2212+
"3.1", message="Starting from Matplotlib 3.3, colorbar() "
2213+
"will steal space from the mappable's axes, rather than "
2214+
"from the current axes, to place the colorbar. To "
2215+
"silence this warning, explicitly pass the 'ax' argument "
2216+
"to colorbar().")
22102217

22112218
# Store the value of gca so that we can set it back later on.
22122219
current_ax = self.gca()
22132220

22142221
if cax is None:
2215-
if use_gridspec and isinstance(ax, SubplotBase) \
2216-
and (not self.get_constrained_layout()):
2222+
if (use_gridspec and isinstance(ax, SubplotBase)
2223+
and not self.get_constrained_layout()):
22172224
cax, kw = cbar.make_axes_gridspec(ax, **kw)
22182225
else:
22192226
cax, kw = cbar.make_axes(ax, **kw)

0 commit comments

Comments
 (0)