Skip to content

Commit 25da964

Browse files
box aspect for axes
1 parent 667a100 commit 25da964

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ def __init__(self, fig, rect,
426426
self.axes = self
427427
self._aspect = 'auto'
428428
self._adjustable = 'box'
429+
self._box_aspect = None
429430
self._anchor = 'C'
430431
self._stale_viewlim_x = False
431432
self._stale_viewlim_y = False
@@ -1282,6 +1283,18 @@ def set_aspect(self, aspect, adjustable=None, anchor=None, share=False):
12821283
self.stale = True
12831284

12841285
def get_adjustable(self):
1286+
"""
1287+
Returns the adjustable parameter, *{'box', 'datalim'}* that defines
1288+
which parameter the Axes will change to achieve a given aspect.
1289+
1290+
See Also
1291+
--------
1292+
matplotlib.axes.Axes.set_adjustable
1293+
defining the parameter to adjust in order to meet the required
1294+
aspect.
1295+
matplotlib.axes.Axes.set_aspect
1296+
for a description of aspect handling.
1297+
"""
12851298
return self._adjustable
12861299

12871300
def set_adjustable(self, adjustable, share=False):
@@ -1333,6 +1346,29 @@ def set_adjustable(self, adjustable, share=False):
13331346
ax._adjustable = adjustable
13341347
self.stale = True
13351348

1349+
def get_box_aspect(self):
1350+
"""
1351+
Get the axes box aspect.
1352+
Will be ``None`` if not explicitely specified.
1353+
"""
1354+
return self._box_aspect
1355+
1356+
def set_box_aspect(self, aspect=None):
1357+
""" Set the axes box aspect. May be ``None`` or a number."""
1358+
axs = {*self._twinned_axes.get_siblings(self),
1359+
*self._twinned_axes.get_siblings(self)}
1360+
1361+
if aspect is not None:
1362+
aspect = float(aspect)
1363+
# when box_aspect is set to other than ´None`,
1364+
# adjustable must be "datalim"
1365+
for ax in axs:
1366+
ax.set_adjustable("datalim")
1367+
1368+
for ax in axs:
1369+
ax._box_aspect = aspect
1370+
ax.stale = True
1371+
13361372
def get_anchor(self):
13371373
"""
13381374
Get the anchor location.
@@ -1464,7 +1500,7 @@ def apply_aspect(self, position=None):
14641500

14651501
aspect = self.get_aspect()
14661502

1467-
if aspect == 'auto':
1503+
if aspect == 'auto' and self._box_aspect is None:
14681504
self._set_position(position, which='active')
14691505
return
14701506

@@ -1484,11 +1520,20 @@ def apply_aspect(self, position=None):
14841520
self._set_position(pb1.anchored(self.get_anchor(), pb), 'active')
14851521
return
14861522

1487-
# self._adjustable == 'datalim'
1523+
# The following is only seen if self._adjustable == 'datalim'
1524+
if self._box_aspect is not None:
1525+
pb = position.frozen()
1526+
pb1 = pb.shrunk_to_aspect(self._box_aspect, pb, fig_aspect)
1527+
self._set_position(pb1.anchored(self.get_anchor(), pb), 'active')
1528+
if aspect == "auto":
1529+
return
14881530

14891531
# reset active to original in case it had been changed by prior use
14901532
# of 'box'
1491-
self._set_position(position, which='active')
1533+
if self._box_aspect is None:
1534+
self._set_position(position, which='active')
1535+
else:
1536+
position = pb1.anchored(self.get_anchor(), pb)
14921537

14931538
x_trf = self.xaxis.get_transform()
14941539
y_trf = self.yaxis.get_transform()

0 commit comments

Comments
 (0)