@@ -1324,7 +1324,7 @@ def set_aspect(self, aspect, adjustable=None, anchor=None, share=False):
1324
1324
self .set_adjustable (adjustable , share = share ) # Handle sharing.
1325
1325
1326
1326
if anchor is not None :
1327
- self .set_anchor (anchor ) # Also needs a " share" kwarg?
1327
+ self .set_anchor (anchor , share = share )
1328
1328
self .stale = True
1329
1329
1330
1330
def get_adjustable (self ):
@@ -1351,10 +1351,22 @@ def set_adjustable(self, adjustable, share=False):
1351
1351
--------
1352
1352
matplotlib.axes.Axes.set_aspect
1353
1353
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.
1354
1366
"""
1355
1367
if adjustable == 'box-forced' :
1356
1368
warnings .warn ("The 'box-forced' keyword argument is deprecated"
1357
- " since 2.2." , mplDeprecation )
1369
+ " since 2.2." , cbook . mplDeprecation )
1358
1370
if adjustable not in ('box' , 'datalim' , 'box-forced' ):
1359
1371
raise ValueError ("argument must be 'box', or 'datalim'" )
1360
1372
if share :
@@ -1379,7 +1391,7 @@ def get_anchor(self):
1379
1391
"""
1380
1392
return self ._anchor
1381
1393
1382
- def set_anchor (self , anchor ):
1394
+ def set_anchor (self , anchor , share = False ):
1383
1395
"""
1384
1396
Define the anchor location.
1385
1397
@@ -1417,16 +1429,26 @@ def set_anchor(self, anchor):
1417
1429
| 'SW' | 'S' | 'SE' |
1418
1430
+------+------+------+
1419
1431
1432
+ share : bool, optional
1433
+ If ``True``, apply the settings to all shared Axes.
1434
+ Default is ``False``.
1435
+
1420
1436
See Also
1421
1437
--------
1422
1438
matplotlib.axes.Axes.set_aspect
1423
1439
for a description of aspect handling.
1424
1440
"""
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 ):
1428
1442
raise ValueError ('argument must be among %s' %
1429
1443
', ' .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
+
1430
1452
self .stale = True
1431
1453
1432
1454
def get_data_ratio (self ):
@@ -1516,6 +1538,9 @@ def apply_aspect(self, position=None):
1516
1538
figW , figH = self .get_figure ().get_size_inches ()
1517
1539
fig_aspect = figH / figW
1518
1540
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." )
1519
1544
if aspect_scale_mode == "log" :
1520
1545
box_aspect = A * self .get_data_ratio_log ()
1521
1546
else :
@@ -4122,13 +4147,16 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
4122
4147
4123
4148
def _make_twin_axes (self , * kl , ** kwargs ):
4124
4149
"""
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.
4126
4151
"""
4127
4152
# 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." )
4128
4156
ax2 = self .figure .add_axes (self .get_position (True ), * kl , ** kwargs )
4129
4157
## 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' )
4132
4160
self ._twinned_axes .join (self , ax2 )
4133
4161
return ax2
4134
4162
0 commit comments