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

Skip to content

Commit eaab865

Browse files
committed
Permit overriding twin axis location with e.g. altx(xloc='side')
1 parent 9784a3a commit eaab865

File tree

1 file changed

+37
-50
lines changed

1 file changed

+37
-50
lines changed

‎proplot/axes/cartesian.py

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,12 @@
171171
Parameters
172172
----------
173173
%(extra)s**kwargs
174-
Passed to `CartesianAxes`. You can optionally omit the {x}
175-
from `CartesianAxes.format` keywords beginning with ``{x}``.
176-
For example: ``ax.alt{x}(lim=(0, 10))`` is equivalent
177-
to ``ax.alt{x}({x}lim=(0, 10))``.
174+
Passed to `CartesianAxes`. Supports all valid `~CartesianAxes.format` keywords.
175+
You can optionally omit the {x} from keywords beginning with ``{x}`` -- for
176+
example ``ax.alt{x}(lim=(0, 10))`` is equivalent to ``ax.alt{x}({x}lim=(0, 10))``.
177+
You can also change the default side for the axis spine, axis tick marks, axis
178+
tick labels, and/or axis labels by passing ``loc`` keywords. For example,
179+
``ax.alt{x}(loc='{x1}')`` changes the default side from {x2} to {x1}.
178180
179181
Returns
180182
-------
@@ -1003,17 +1005,16 @@ def format(
10031005
self.set_aspect(aspect)
10041006
super().format(rc_kw=rc_kw, rc_mode=rc_mode, **kwargs)
10051007

1006-
def _parse_alt(self, kwargs, x):
1008+
def _parse_alt(self, x, **kwargs):
10071009
"""
10081010
Optionally omit the leading x or y from "twin axes" methods.
10091011
"""
1010-
keys = tuple(
1011-
key[1:] for key in self._format_signature.parameters if key[0] == x
1012-
)
1013-
kw = {
1014-
(x + key if key in keys else key): val for key, val in kwargs.items()
1015-
}
1016-
return kw
1012+
keys = tuple(k[1:] for k in self._format_signature.parameters if k[0] == x)
1013+
kwargs = {(x + k if k in keys else k): v for k, v in kwargs.items()}
1014+
for axis in 'xy': # standardize format() location aliases
1015+
if axis + 'spineloc' in kwargs:
1016+
kwargs[axis + 'loc'] = kwargs.pop(axis + 'spineloc')
1017+
return kwargs
10171018

10181019
@docstring._snippet_manager
10191020
def altx(self, **kwargs):
@@ -1028,32 +1029,24 @@ def altx(self, **kwargs):
10281029
# To restore matplotlib behavior, which draws "child" artists on top simply
10291030
# because the axes was created after the "parent" one, use the inset_axes
10301031
# zorder of 4 and make the background transparent.
1031-
kwargs = self._parse_alt(kwargs, 'x')
1032+
kwargs = self._parse_alt('x', **kwargs)
1033+
kwargs.setdefault('xloc', 'right') # other locations follow by default
1034+
kwargs.setdefault('grid', False) # note xgrid=True would override this
1035+
kwargs.setdefault('zorder', 4)
1036+
kwargs.setdefault('autoscaley_on', self.get_autoscaley_on())
1037+
1038+
# Initialize twin axes
10321039
ax = self._make_twin_axes(
10331040
sharey=self, number=False, autoshare=False, projection='cartesian', **kwargs
10341041
)
1035-
1036-
# Child defaults
1037-
# NOTE: This is not completely ideal since e.g. users passing labelloc
1038-
# will get overwritten. But mostly not a huge deal.
10391042
ax._altx_parent = self
1040-
for side, spine in ax.spines.items():
1041-
spine.set_visible(side == 'top')
1042-
ax.xaxis.tick_top()
1043-
ax.xaxis.set_label_position('top')
1044-
ax.yaxis.set_visible(False)
10451043
ax.patch.set_visible(False)
1046-
ax.grid(False)
1047-
ax.set_zorder(4)
1048-
ax.set_autoscaley_on(self.get_autoscaley_on())
1044+
ax.yaxis.set_visible(False)
10491045

10501046
# Parent defaults
1051-
self.spines['top'].set_visible(False)
1052-
self.spines['bottom'].set_visible(True)
1053-
self.xaxis.tick_bottom()
1054-
self.xaxis.set_label_position('bottom')
1055-
1056-
# Add axes
1047+
reverse = {'bottom': 'top', 'top': 'bottom'}
1048+
kwformat = {'xloc': reverse.get(kwargs['xloc'], None)}
1049+
self.format(**kwformat)
10571050
self.add_child_axes(ax) # to facilitate tight layout
10581051
self.figure._axstack.remove(ax) # or gets drawn twice!
10591052

@@ -1064,31 +1057,25 @@ def alty(self, **kwargs):
10641057
"""
10651058
%(axes.alty)s
10661059
"""
1067-
# Initialize axes
1068-
kwargs = self._parse_alt(kwargs, 'y')
1060+
# Parse input args
1061+
kwargs = self._parse_alt('y', **kwargs)
1062+
kwargs.setdefault('yloc', 'right') # other locations follow by default
1063+
kwargs.setdefault('grid', False) # note ygrid=True would override this
1064+
kwargs.setdefault('zorder', 4)
1065+
kwargs.setdefault('autoscalex_on', self.get_autoscalex_on())
1066+
1067+
# Initialize twin axes
10691068
ax = self._make_twin_axes(
10701069
sharex=self, number=False, autoshare=False, projection='cartesian', **kwargs
10711070
)
1072-
1073-
# Child defaults
10741071
ax._alty_parent = self
1075-
for side, spine in ax.spines.items():
1076-
spine.set_visible(side == 'right')
1077-
ax.yaxis.tick_right()
1078-
ax.yaxis.set_label_position('right')
1079-
ax.xaxis.set_visible(False)
10801072
ax.patch.set_visible(False)
1081-
ax.grid(False)
1082-
ax.set_zorder(4)
1083-
ax.set_autoscalex_on(self.get_autoscalex_on())
1084-
1085-
# Parent defaults
1086-
self.spines['right'].set_visible(False)
1087-
self.spines['left'].set_visible(True)
1088-
self.yaxis.tick_left()
1089-
self.yaxis.set_label_position('left')
1073+
ax.xaxis.set_visible(False)
10901074

1091-
# Add axes
1075+
# Update parent axes
1076+
reverse = {'left': 'right', 'right': 'left'}
1077+
kwformat = {'yloc': reverse.get(kwargs['yloc'], None)}
1078+
self.format(**kwformat)
10921079
self.add_child_axes(ax) # to facilitate tight layout
10931080
self.figure._axstack.remove(ax) # or gets drawn twice!
10941081

0 commit comments

Comments
 (0)