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

Skip to content

Commit 2371be7

Browse files
committed
More guards and documentation for valid 'adjustable' values.
1 parent e9e1c69 commit 2371be7

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ def set_aspect(self, aspect, adjustable=None, anchor=None, share=False):
13241324
self.set_adjustable(adjustable, share=share) # Handle sharing.
13251325

13261326
if anchor is not None:
1327-
self.set_anchor(anchor) # Also needs a "share" kwarg?
1327+
self.set_anchor(anchor, share=share)
13281328
self.stale = True
13291329

13301330
def get_adjustable(self):
@@ -1351,10 +1351,22 @@ def set_adjustable(self, adjustable, share=False):
13511351
--------
13521352
matplotlib.axes.Axes.set_aspect
13531353
for a description of aspect handling.
1354+
1355+
Notes
1356+
-----
1357+
Shared Axes (of which twinned Axes are a special case)
1358+
impose restrictions on how aspect ratios can be imposed.
1359+
For twinned Axes, use 'datalim'. For Axes that share both
1360+
x and y, use 'box'. Otherwise, either 'datalim' or 'box'
1361+
may be used. These limitations are partly a requirement
1362+
to avoid over-specification, and partly a result of the
1363+
particular implementation we are currently using, in
1364+
which the adjustments for aspect ratios are done sequentially
1365+
and independently on each Axes as it is drawn.
13541366
"""
13551367
if adjustable == 'box-forced':
13561368
warnings.warn("The 'box-forced' keyword argument is deprecated"
1357-
" since 2.2.", mplDeprecation)
1369+
" since 2.2.", cbook.mplDeprecation)
13581370
if adjustable not in ('box', 'datalim', 'box-forced'):
13591371
raise ValueError("argument must be 'box', or 'datalim'")
13601372
if share:
@@ -1379,7 +1391,7 @@ def get_anchor(self):
13791391
"""
13801392
return self._anchor
13811393

1382-
def set_anchor(self, anchor):
1394+
def set_anchor(self, anchor, share=False):
13831395
"""
13841396
Define the anchor location.
13851397
@@ -1417,16 +1429,26 @@ def set_anchor(self, anchor):
14171429
| 'SW' | 'S' | 'SE' |
14181430
+------+------+------+
14191431
1432+
share : bool, optional
1433+
If ``True``, apply the settings to all shared Axes.
1434+
Default is ``False``.
1435+
14201436
See Also
14211437
--------
14221438
matplotlib.axes.Axes.set_aspect
14231439
for a description of aspect handling.
14241440
"""
1425-
if anchor in mtransforms.Bbox.coefs or len(anchor) == 2:
1426-
self._anchor = anchor
1427-
else:
1441+
if not (anchor in mtransforms.Bbox.coefs or len(anchor) == 2):
14281442
raise ValueError('argument must be among %s' %
14291443
', '.join(mtransforms.Bbox.coefs))
1444+
if share:
1445+
axes = set(self._shared_x_axes.get_siblings(self)
1446+
+ self._shared_y_axes.get_siblings(self))
1447+
else:
1448+
axes = [self]
1449+
for ax in axes:
1450+
ax._anchor = anchor
1451+
14301452
self.stale = True
14311453

14321454
def get_data_ratio(self):
@@ -1516,6 +1538,9 @@ def apply_aspect(self, position=None):
15161538
figW, figH = self.get_figure().get_size_inches()
15171539
fig_aspect = figH / figW
15181540
if self._adjustable in ['box', 'box-forced']:
1541+
if self in self._twinned_axes:
1542+
raise RuntimeError("Adjustable 'box' is not allowed in a"
1543+
" twinned Axes. Use 'datalim' instead.")
15191544
if aspect_scale_mode == "log":
15201545
box_aspect = A * self.get_data_ratio_log()
15211546
else:
@@ -4122,13 +4147,16 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
41224147

41234148
def _make_twin_axes(self, *kl, **kwargs):
41244149
"""
4125-
make a twinx axes of self. This is used for twinx and twiny.
4150+
Make a twinx axes of self. This is used for twinx and twiny.
41264151
"""
41274152
# Typically, SubplotBase._make_twin_axes is called instead of this.
4153+
# There is also an override in axes_grid1/axes_divider.py.
4154+
if 'sharex' in kwargs and 'sharey' in kwargs:
4155+
raise ValueError("Twinned Axes may share only one axis.")
41284156
ax2 = self.figure.add_axes(self.get_position(True), *kl, **kwargs)
41294157
## do not touch every thing shared, just this and it's twin.
4130-
#self.set_adjustable('datalim')
4131-
#ax2.set_adjustable('datalim')
4158+
self.set_adjustable('datalim')
4159+
ax2.set_adjustable('datalim')
41324160
self._twinned_axes.join(self, ax2)
41334161
return ax2
41344162

lib/matplotlib/axes/_subplots.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,20 @@ def label_outer(self):
144144

145145
def _make_twin_axes(self, *kl, **kwargs):
146146
"""
147-
make a twinx axes of self. This is used for twinx and twiny.
147+
Make a twinx axes of self. This is used for twinx and twiny.
148148
"""
149149
from matplotlib.projections import process_projection_requirements
150+
if 'sharex' in kwargs and 'sharey' in kwargs:
151+
raise ValueError("Twinned Axes may share only one axis.")
150152
kl = (self.get_subplotspec(),) + kl
151153
projection_class, kwargs, key = process_projection_requirements(
152154
self.figure, *kl, **kwargs)
153155

154156
ax2 = subplot_class_factory(projection_class)(self.figure,
155157
*kl, **kwargs)
156158
self.figure.add_subplot(ax2)
159+
self.set_adjustable('datalim')
160+
ax2.set_adjustable('datalim')
157161
self._twinned_axes.join(self, ax2)
158162
return ax2
159163

lib/mpl_toolkits/axes_grid1/axes_divider.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,9 +915,13 @@ def _make_twin_axes(self, *kl, **kwargs):
915915
Need to overload so that twinx/twiny will work with
916916
these axes.
917917
"""
918+
if 'sharex' in kwargs and 'sharey' in kwargs:
919+
raise ValueError("Twinned Axes may share only one axis.")
918920
ax2 = type(self)(self.figure, self.get_position(True), *kl, **kwargs)
919921
ax2.set_axes_locator(self.get_axes_locator())
920922
self.figure.add_axes(ax2)
923+
self.set_adjustable('datalim')
924+
ax2.set_adjustable('datalim')
921925
self._twinned_axes.join(self, ax2)
922926
return ax2
923927

0 commit comments

Comments
 (0)