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

Skip to content

Commit bdcaa9c

Browse files
committed
Deprecate Axes.apply_aspect()
1 parent ecba9d2 commit bdcaa9c

File tree

9 files changed

+58
-25
lines changed

9 files changed

+58
-25
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The parameter *position* in ``Axes.apply_aspect()``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... is deprecated. It is considered internal API and now public use case is known.

lib/matplotlib/_tight_bbox.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ def adjust_bbox(fig, bbox_inches, fixed_dpi=None):
2929
current_pos = ax.get_position(original=False).frozen()
3030
ax.set_axes_locator(lambda a, r, _pos=current_pos: _pos)
3131
# override the method that enforces the aspect ratio on the Axes
32-
if 'apply_aspect' in ax.__dict__:
33-
old_aspect.append(ax.apply_aspect)
32+
if '_apply_aspect' in ax.__dict__:
33+
old_aspect.append(ax._apply_aspect)
3434
else:
3535
old_aspect.append(sentinel)
36-
ax.apply_aspect = lambda pos=None: None
36+
ax._apply_aspect = lambda pos=None: None
3737

3838
def restore_bbox():
3939
for ax, loc, aspect in zip(fig.axes, locator_list, old_aspect):
4040
ax.set_axes_locator(loc)
4141
if aspect is sentinel:
4242
# delete our no-op function which un-hides the original method
43-
del ax.apply_aspect
43+
del ax._apply_aspect
4444
else:
45-
ax.apply_aspect = aspect
45+
ax._apply_aspect = aspect
4646

4747
fig.bbox = origBbox
4848
fig.bbox_inches = origBboxInches

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs):
376376
inset_ax = projection_class(self.figure, bounds, zorder=zorder, **pkw)
377377

378378
# this locator lets the axes move if in data coordinates.
379-
# it gets called in `ax.apply_aspect() (of all places)
379+
# it gets called in `ax._apply_aspect() (of all places)
380380
inset_ax.set_axes_locator(inset_locator)
381381

382382
self.add_child_axes(inset_ax)

lib/matplotlib/axes/_base.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,7 @@ def set_adjustable(self, adjustable, share=False):
17191719
and any(getattr(ax.get_data_ratio, "__func__", None)
17201720
!= _AxesBase.get_data_ratio
17211721
for ax in axs)):
1722-
# Limits adjustment by apply_aspect assumes that the axes' aspect
1722+
# Limits adjustment by _apply_aspect assumes that the axes' aspect
17231723
# ratio can be computed from the data limits and scales.
17241724
raise ValueError("Cannot set Axes adjustable to 'datalim' for "
17251725
"Axes which override 'get_data_ratio'")
@@ -1855,6 +1855,7 @@ def get_data_ratio(self):
18551855
ysize = max(abs(tymax - tymin), 1e-30)
18561856
return ysize / xsize
18571857

1858+
@_api.delete_parameter("3.6", "position")
18581859
def apply_aspect(self, position=None):
18591860
"""
18601861
Adjust the Axes for a specified data aspect ratio.
@@ -1866,10 +1867,17 @@ def apply_aspect(self, position=None):
18661867
Parameters
18671868
----------
18681869
position : None or .Bbox
1870+
18691871
If not ``None``, this defines the position of the
18701872
Axes within the figure as a Bbox. See `~.Axes.get_position`
18711873
for further details.
18721874
1875+
.. admonition:: Deprecated
1876+
1877+
Changing the *position* through ``apply_aspect`` is
1878+
considered internal API. This parameter will be removed
1879+
in the future.
1880+
18731881
Notes
18741882
-----
18751883
This is called automatically when each Axes is drawn. You may need
@@ -1885,6 +1893,24 @@ def apply_aspect(self, position=None):
18851893
matplotlib.axes.Axes.set_anchor
18861894
Set the position in case of extra space.
18871895
"""
1896+
# Note: This method is a thin wrapper that only exists for the purpose
1897+
# of not exposing the position parameter as public API.
1898+
self._apply_aspect(position)
1899+
1900+
def _apply_aspect(self, position=None):
1901+
"""
1902+
Adjust the Axes for a specified data aspect ratio.
1903+
1904+
See the docstring of the public `apply_aspect` method.
1905+
1906+
Parameters
1907+
----------
1908+
position : None or .Bbox
1909+
If not ``None``, this defines the position of the
1910+
Axes within the figure as a Bbox. See `~.Axes.get_position`
1911+
for further details.
1912+
1913+
"""
18881914
if position is None:
18891915
position = self.get_position(original=True)
18901916

@@ -2954,7 +2980,7 @@ def _update_title_position(self, renderer):
29542980
axs = self._twinned_axes.get_siblings(self) + self.child_axes
29552981
for ax in self.child_axes: # Child positions must be updated first.
29562982
locator = ax.get_axes_locator()
2957-
ax.apply_aspect(locator(self, renderer) if locator else None)
2983+
ax._apply_aspect(locator(self, renderer) if locator else None)
29582984

29592985
for title in titles:
29602986
x, _ = title.get_position()
@@ -3017,7 +3043,7 @@ def draw(self, renderer):
30173043

30183044
# loop over self and child Axes...
30193045
locator = self.get_axes_locator()
3020-
self.apply_aspect(locator(self, renderer) if locator else None)
3046+
self._apply_aspect(locator(self, renderer) if locator else None)
30213047

30223048
artists = self.get_children()
30233049
artists.remove(self.patch)
@@ -4382,7 +4408,7 @@ def get_tightbbox(self, renderer=None, call_axes_locator=True,
43824408
return None
43834409

43844410
locator = self.get_axes_locator()
4385-
self.apply_aspect(
4411+
self._apply_aspect(
43864412
locator(self, renderer) if locator and call_axes_locator else None)
43874413

43884414
for axis in self._axis_map.values():

lib/matplotlib/axes/_secondary_axes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ def set_location(self, location):
114114
# this locator lets the axes move in the parent axes coordinates.
115115
# so it never needs to know where the parent is explicitly in
116116
# figure coordinates.
117-
# it gets called in ax.apply_aspect() (of all places)
117+
# it gets called in ax._apply_aspect() (of all places)
118118
self.set_axes_locator(
119119
_TransformedBoundsLocator(bounds, self._parent.transAxes))
120120

121-
def apply_aspect(self, position=None):
121+
def _apply_aspect(self, position=None):
122122
# docstring inherited.
123123
self._set_lims()
124-
super().apply_aspect(position)
124+
super()._apply_aspect(position)
125125

126126
@_docstring.copy(Axis.set_ticks)
127127
def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs):

lib/matplotlib/figure.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def __init__(self, **kwargs):
193193
self.set(**kwargs)
194194

195195
def _get_draw_artists(self, renderer):
196-
"""Also runs apply_aspect"""
196+
"""Also runs _apply_aspect"""
197197
artists = self.get_children()
198198
for sfig in self.subfigs:
199199
artists.remove(sfig)
@@ -208,12 +208,12 @@ def _get_draw_artists(self, renderer):
208208
key=lambda artist: artist.get_zorder())
209209
for ax in self._localaxes:
210210
locator = ax.get_axes_locator()
211-
ax.apply_aspect(locator(ax, renderer) if locator else None)
211+
ax._apply_aspect(locator(ax, renderer) if locator else None)
212212

213213
for child in ax.get_children():
214-
if hasattr(child, 'apply_aspect'):
214+
if hasattr(child, '_apply_aspect'):
215215
locator = child.get_axes_locator()
216-
child.apply_aspect(
216+
child._apply_aspect(
217217
locator(child, renderer) if locator else None)
218218
return artists
219219

lib/matplotlib/tests/test_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5273,7 +5273,7 @@ def test_shared_with_aspect_2():
52735273
axs[0].set_aspect(2, share=True)
52745274
axs[0].plot([1, 2], [3, 4])
52755275
axs[1].plot([3, 4], [1, 2])
5276-
plt.draw() # Trigger apply_aspect().
5276+
plt.draw() # Trigger _apply_aspect().
52775277
assert axs[0].get_xlim() == axs[1].get_xlim()
52785278
assert axs[0].get_ylim() == axs[1].get_ylim()
52795279

@@ -5286,7 +5286,7 @@ def test_shared_with_aspect_3():
52865286
axs[1].set_aspect(0.5, adjustable=adjustable)
52875287
axs[0].plot([1, 2], [3, 4])
52885288
axs[1].plot([3, 4], [1, 2])
5289-
plt.draw() # Trigger apply_aspect().
5289+
plt.draw() # Trigger _apply_aspect().
52905290
assert axs[0].get_xlim() != axs[1].get_xlim()
52915291
assert axs[0].get_ylim() == axs[1].get_ylim()
52925292
fig_aspect = fig.bbox_inches.height / fig.bbox_inches.width

lib/mpl_toolkits/axes_grid1/parasite_axes.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,13 @@ def _update_viewlim(self): # Inline after deprecation elapses.
8282
else:
8383
_api.check_in_list([None, "equal", "transform"], mode=mode)
8484

85+
@_api.delete_parameter("3.6", "position")
8586
def apply_aspect(self, position=None):
87+
self._apply_aspect(position)
88+
89+
def _apply_aspect(self, position=None):
8690
self._update_viewlim()
87-
super().apply_aspect()
91+
super()._apply_aspect()
8892

8993
# end of aux_transform support
9094

@@ -126,13 +130,13 @@ def draw(self, renderer):
126130
if locator:
127131
pos = locator(self, renderer)
128132
self.set_position(pos, which="active")
129-
self.apply_aspect(pos)
133+
self._apply_aspect(pos)
130134
else:
131-
self.apply_aspect()
135+
self._apply_aspect()
132136

133137
rect = self.get_position()
134138
for ax in self.parasites:
135-
ax.apply_aspect(rect)
139+
ax._apply_aspect(rect)
136140
self._children.extend(ax.get_children())
137141

138142
super().draw(renderer)

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def set_box_aspect(self, aspect, *, zoom=1):
387387
self._box_aspect = aspect
388388
self.stale = True
389389

390-
def apply_aspect(self, position=None):
390+
def _apply_aspect(self, position=None):
391391
if position is None:
392392
position = self.get_position(original=True)
393393

@@ -420,7 +420,7 @@ def draw(self, renderer):
420420
# it adjusts the view limits and the size of the bounding box
421421
# of the Axes
422422
locator = self.get_axes_locator()
423-
self.apply_aspect(locator(self, renderer) if locator else None)
423+
self._apply_aspect(locator(self, renderer) if locator else None)
424424

425425
# add the projection matrix to the renderer
426426
self.M = self.get_proj()

0 commit comments

Comments
 (0)