Skip to content

Commit 81e9559

Browse files
authored
Merge pull request #22412 from timhoffm/propertize
Turn _get_axis_map() into a property and remove _get_axis_list()
2 parents b80bc91 + 1b61cc7 commit 81e9559

File tree

5 files changed

+39
-42
lines changed

5 files changed

+39
-42
lines changed

lib/matplotlib/artist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def remove(self):
235235
def have_units(self):
236236
"""Return whether units are set on any axis."""
237237
ax = self.axes
238-
return ax and any(axis.have_units() for axis in ax._get_axis_list())
238+
return ax and any(axis.have_units() for axis in ax._axis_map.values())
239239

240240
def convert_xunits(self, x):
241241
"""

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5590,7 +5590,7 @@ def _interp_grid(X):
55905590

55915591
def _pcolor_grid_deprecation_helper(self):
55925592
grid_active = any(axis._major_tick_kw["gridOn"]
5593-
for axis in self._get_axis_list())
5593+
for axis in self._axis_map.values())
55945594
# explicit is-True check because get_axisbelow() can also be 'line'
55955595
grid_hidden_by_pcolor = self.get_axisbelow() is True
55965596
if grid_active and not grid_hidden_by_pcolor:

lib/matplotlib/axes/_base.py

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -541,10 +541,23 @@ def _plot_args(self, tup, kwargs, return_kwargs=False):
541541
class _AxesBase(martist.Artist):
542542
name = "rectilinear"
543543

544-
_axis_names = ("x", "y") # See _get_axis_map.
544+
# axis names are the prefixes for the attributes that contain the
545+
# respective axis; e.g. 'x' <-> self.xaxis, containing an XAxis.
546+
# Note that PolarAxes uses these attributes as well, so that we have
547+
# 'x' <-> self.xaxis, containing a ThetaAxis. In particular we do not
548+
# have 'theta' in _axis_names.
549+
# In practice, this is ('x', 'y') for all 2D Axes and ('x', 'y', 'z')
550+
# for Axes3D.
551+
_axis_names = ("x", "y")
545552
_shared_axes = {name: cbook.Grouper() for name in _axis_names}
546553
_twinned_axes = cbook.Grouper()
547554

555+
@property
556+
def _axis_map(self):
557+
"""A mapping of axis names, e.g. 'x', to `Axis` instances."""
558+
return {name: getattr(self, f"{name}axis")
559+
for name in self._axis_names}
560+
548561
def __str__(self):
549562
return "{0}({1[0]:g},{1[1]:g};{1[2]:g}x{1[3]:g})".format(
550563
type(self).__name__, self._position.bounds)
@@ -644,7 +657,7 @@ def __init__(self, fig, rect,
644657

645658
self._internal_update(kwargs)
646659

647-
for name, axis in self._get_axis_map().items():
660+
for name, axis in self._axis_map.items():
648661
axis.callbacks._pickled_cids.add(
649662
axis.callbacks.connect(
650663
'units', self._unit_change_handler(name)))
@@ -2437,7 +2450,7 @@ def _unit_change_handler(self, axis_name, event=None):
24372450
if event is None: # Allow connecting `self._unit_change_handler(name)`
24382451
return functools.partial(
24392452
self._unit_change_handler, axis_name, event=object())
2440-
_api.check_in_list(self._get_axis_map(), axis_name=axis_name)
2453+
_api.check_in_list(self._axis_map, axis_name=axis_name)
24412454
for line in self.lines:
24422455
line.recache_always()
24432456
self.relim()
@@ -2503,7 +2516,7 @@ def _process_unit_info(self, datasets=None, kwargs=None, *, convert=True):
25032516
----------
25042517
datasets : list
25052518
List of (axis_name, dataset) pairs (where the axis name is defined
2506-
as in `._get_axis_map`). Individual datasets can also be None
2519+
as in `._axis_map`). Individual datasets can also be None
25072520
(which gets passed through).
25082521
kwargs : dict
25092522
Other parameters from which unit info (i.e., the *xunits*,
@@ -2525,7 +2538,7 @@ def _process_unit_info(self, datasets=None, kwargs=None, *, convert=True):
25252538
# (e.g. if some are scalars, etc.).
25262539
datasets = datasets or []
25272540
kwargs = kwargs or {}
2528-
axis_map = self._get_axis_map()
2541+
axis_map = self._axis_map
25292542
for axis_name, data in datasets:
25302543
try:
25312544
axis = axis_map[axis_name]
@@ -2953,22 +2966,6 @@ def handle_single_axis(scale, autoscaleon, shared_axes, name,
29532966
scaley, self._autoscaleYon, self._shared_axes["y"], 'y',
29542967
self.yaxis, self._ymargin, y_stickies, self.set_ybound)
29552968

2956-
def _get_axis_list(self):
2957-
return tuple(getattr(self, f"{name}axis") for name in self._axis_names)
2958-
2959-
def _get_axis_map(self):
2960-
"""
2961-
Return a mapping of `Axis` "names" to `Axis` instances.
2962-
2963-
The `Axis` name is derived from the attribute under which the instance
2964-
is stored, so e.g. for polar Axes, the theta-axis is still named "x"
2965-
and the r-axis is still named "y" (for back-compatibility).
2966-
2967-
In practice, this means that the entries are typically "x" and "y", and
2968-
additionally "z" for 3D Axes.
2969-
"""
2970-
return dict(zip(self._axis_names, self._get_axis_list()))
2971-
29722969
def _update_title_position(self, renderer):
29732970
"""
29742971
Update the title position based on the bounding box enclosing
@@ -3069,7 +3066,7 @@ def draw(self, renderer):
30693066
self._update_title_position(renderer)
30703067

30713068
if not self.axison:
3072-
for _axis in self._get_axis_list():
3069+
for _axis in self._axis_map.values():
30733070
artists.remove(_axis)
30743071

30753072
if not self.figure.canvas.is_saving():
@@ -3131,7 +3128,7 @@ def redraw_in_frame(self):
31313128
raise AttributeError("redraw_in_frame can only be used after an "
31323129
"initial draw which caches the renderer")
31333130
with ExitStack() as stack:
3134-
for artist in [*self._get_axis_list(),
3131+
for artist in [*self._axis_map.values(),
31353132
self.title, self._left_title, self._right_title]:
31363133
stack.enter_context(artist._cm_set(visible=False))
31373134
self.draw(self.figure._cachedRenderer)
@@ -3202,7 +3199,7 @@ def set_axisbelow(self, b):
32023199
zorder = 1.5
32033200
else:
32043201
raise ValueError("Unexpected axisbelow value")
3205-
for axis in self._get_axis_list():
3202+
for axis in self._axis_map.values():
32063203
axis.set_zorder(zorder)
32073204
self.stale = True
32083205

@@ -3306,8 +3303,8 @@ def ticklabel_format(self, *, axis='both', style='', scilimits=None,
33063303
) from err
33073304
STYLES = {'sci': True, 'scientific': True, 'plain': False, '': None}
33083305
is_sci_style = _api.check_getitem(STYLES, style=style)
3309-
axis_map = {**{k: [v] for k, v in self._get_axis_map().items()},
3310-
'both': self._get_axis_list()}
3306+
axis_map = {**{k: [v] for k, v in self._axis_map.items()},
3307+
'both': list(self._axis_map.values())}
33113308
axises = _api.check_getitem(axis_map, axis=axis)
33123309
try:
33133310
for axis in axises:
@@ -3361,7 +3358,7 @@ def locator_params(self, axis='both', tight=None, **kwargs):
33613358
_api.check_in_list([*self._axis_names, "both"], axis=axis)
33623359
for name in self._axis_names:
33633360
if axis in [name, "both"]:
3364-
loc = self._get_axis_map()[name].get_major_locator()
3361+
loc = self._axis_map[name].get_major_locator()
33653362
loc.set_params(**kwargs)
33663363
self._request_autoscale_view(name, tight=tight)
33673364
self.stale = True
@@ -4412,7 +4409,7 @@ def get_children(self):
44124409
return [
44134410
*self._children,
44144411
*self.spines.values(),
4415-
*self._get_axis_list(),
4412+
*self._axis_map.values(),
44164413
self.title, self._left_title, self._right_title,
44174414
*self.child_axes,
44184415
*([self.legend_] if self.legend_ is not None else []),
@@ -4444,10 +4441,10 @@ def get_default_bbox_extra_artists(self):
44444441

44454442
artists = self.get_children()
44464443

4447-
for _axis in self._get_axis_list():
4444+
for axis in self._axis_map.values():
44484445
# axis tight bboxes are calculated separately inside
44494446
# Axes.get_tightbbox() using for_layout_only=True
4450-
artists.remove(_axis)
4447+
artists.remove(axis)
44514448
if not (self.axison and self._frameon):
44524449
# don't do bbox on spines if frame not on.
44534450
for spine in self.spines.values():
@@ -4520,7 +4517,7 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
45204517
else:
45214518
self.apply_aspect()
45224519

4523-
for axis in self._get_axis_list():
4520+
for axis in self._axis_map.values():
45244521
if self.axison and axis.get_visible():
45254522
ba = martist._get_tightbbox_for_layout_only(axis, renderer)
45264523
if ba:

lib/matplotlib/axis.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ def _set_lim(self, v0, v1, *, emit=True, auto):
10461046
Whether to turn on autoscaling of the x-axis. True turns on, False
10471047
turns off, None leaves unchanged.
10481048
"""
1049-
name, = [name for name, axis in self.axes._get_axis_map().items()
1049+
name, = [name for name, axis in self.axes._axis_map.items()
10501050
if axis is self] # The axis name.
10511051

10521052
self.axes._process_unit_info([(name, (v0, v1))], convert=False)
@@ -1095,7 +1095,7 @@ def _set_lim(self, v0, v1, *, emit=True, auto):
10951095
# Call all of the other axes that are shared with this one
10961096
for other in self.axes._shared_axes[name].get_siblings(self.axes):
10971097
if other is not self.axes:
1098-
other._get_axis_map()[name]._set_lim(
1098+
other._axis_map[name]._set_lim(
10991099
v0, v1, emit=False, auto=auto)
11001100
if other.figure != self.figure:
11011101
other.figure.canvas.draw_idle()
@@ -1604,7 +1604,7 @@ def set_units(self, u):
16041604
"""
16051605
if u == self.units:
16061606
return
1607-
for name, axis in self.axes._get_axis_map().items():
1607+
for name, axis in self.axes._axis_map.items():
16081608
if self is axis:
16091609
shared = [
16101610
getattr(ax, f"{name}axis")
@@ -1883,7 +1883,7 @@ def _set_tick_locations(self, ticks, *, minor=False):
18831883

18841884
# XXX if the user changes units, the information will be lost here
18851885
ticks = self.convert_units(ticks)
1886-
for name, axis in self.axes._get_axis_map().items():
1886+
for name, axis in self.axes._axis_map.items():
18871887
if self is axis:
18881888
shared = [
18891889
getattr(ax, f"{name}axis")
@@ -1952,7 +1952,7 @@ def _get_tick_boxes_siblings(self, renderer):
19521952
"""
19531953
# Get the Grouper keeping track of x or y label groups for this figure.
19541954
axis_names = [
1955-
name for name, axis in self.axes._get_axis_map().items()
1955+
name for name, axis in self.axes._axis_map.items()
19561956
if name in self.figure._align_label_groups and axis is self]
19571957
if len(axis_names) != 1:
19581958
return [], []

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def draw(self, renderer):
424424
# Calculate projection of collections and patches and zorder
425425
# them. Make sure they are drawn above the grids.
426426
zorder_offset = max(axis.get_zorder()
427-
for axis in self._get_axis_list()) + 1
427+
for axis in self._axis_map.values()) + 1
428428
collection_zorder = patch_zorder = zorder_offset
429429

430430
for artist in sorted(collections_and_patches,
@@ -442,10 +442,10 @@ def draw(self, renderer):
442442

443443
if self._axis3don:
444444
# Draw panes first
445-
for axis in self._get_axis_list():
445+
for axis in self._axis_map.values():
446446
axis.draw_pane(renderer)
447447
# Then axes
448-
for axis in self._get_axis_list():
448+
for axis in self._axis_map.values():
449449
axis.draw(renderer)
450450

451451
# Then rest
@@ -3165,7 +3165,7 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
31653165
for_layout_only=for_layout_only)
31663166
batch = [ret]
31673167
if self._axis3don:
3168-
for axis in self._get_axis_list():
3168+
for axis in self._axis_map.values():
31693169
if axis.get_visible():
31703170
axis_bb = martist._get_tightbbox_for_layout_only(
31713171
axis, renderer)

0 commit comments

Comments
 (0)