Skip to content

Commit 0c072a5

Browse files
committed
Do not clear Axis when registering spine
1 parent 0b8bd96 commit 0c072a5

File tree

8 files changed

+44
-32
lines changed

8 files changed

+44
-32
lines changed

galleries/examples/specialty_plots/radar_chart.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,17 @@ def _gen_axes_spines(self):
9999
return super()._gen_axes_spines()
100100
elif frame == 'polygon':
101101
# spine_type must be 'left'/'right'/'top'/'bottom'/'circle'.
102-
spine = Spine(axes=self,
103-
spine_type='circle',
104-
path=Path.unit_regular_polygon(num_vars))
102+
polar_spine = Spine(axes=self,
103+
spine_type='circle',
104+
path=Path.unit_regular_polygon(num_vars))
105105
# unit_regular_polygon gives a polygon of radius 1 centered at
106106
# (0, 0) but we want a polygon of radius 0.5 centered at (0.5,
107107
# 0.5) in axes coordinates.
108-
spine.set_transform(Affine2D().scale(.5).translate(.5, .5)
109-
+ self.transAxes)
110-
return {'polar': spine}
108+
polar_spine.set_transform(Affine2D().scale(.5).translate(.5, .5)
109+
+ self.transAxes)
110+
# Must have a dummy spine for 'inner'
111+
inner_spine = Spine.linear_spine(self, 'bottom')
112+
return {'polar': polar_spine, 'inner': inner_spine}
111113
else:
112114
raise ValueError("Unknown value for 'frame': %s" % frame)
113115

lib/matplotlib/axes/_base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -814,10 +814,10 @@ def get_window_extent(self, renderer=None):
814814

815815
def _init_axis(self):
816816
# This is moved out of __init__ because non-separable axes don't use it
817-
self.xaxis = maxis.XAxis(self)
817+
self.xaxis = maxis.XAxis(self, clear=False)
818818
self.spines.bottom.register_axis(self.xaxis)
819819
self.spines.top.register_axis(self.xaxis)
820-
self.yaxis = maxis.YAxis(self)
820+
self.yaxis = maxis.YAxis(self, clear=False)
821821
self.spines.left.register_axis(self.yaxis)
822822
self.spines.right.register_axis(self.yaxis)
823823

@@ -1275,7 +1275,7 @@ def __clear(self):
12751275
for axis in self._axis_map.values():
12761276
axis.clear() # Also resets the scale to linear.
12771277
for spine in self.spines.values():
1278-
spine.clear()
1278+
spine._clear() # Use _clear to not clear Axis again
12791279

12801280
self.ignore_existing_data_limits = True
12811281
self.callbacks = cbook.CallbackRegistry(

lib/matplotlib/axis.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,10 @@ def __init__(
8787
super().__init__()
8888

8989
if gridOn is None:
90-
if major and (mpl.rcParams['axes.grid.which']
91-
in ('both', 'major')):
90+
which = mpl.rcParams['axes.grid.which']
91+
if major and (which in ('both', 'major')):
9292
gridOn = mpl.rcParams['axes.grid']
93-
elif (not major) and (mpl.rcParams['axes.grid.which']
94-
in ('both', 'minor')):
93+
elif (not major) and (which in ('both', 'minor')):
9594
gridOn = mpl.rcParams['axes.grid']
9695
else:
9796
gridOn = False
@@ -632,7 +631,7 @@ def __str__(self):
632631
return "{}({},{})".format(
633632
type(self).__name__, *self.axes.transAxes.transform((0, 0)))
634633

635-
def __init__(self, axes, *, pickradius=15):
634+
def __init__(self, axes, *, pickradius=15, clear=True):
636635
"""
637636
Parameters
638637
----------
@@ -641,6 +640,8 @@ def __init__(self, axes, *, pickradius=15):
641640
pickradius : float
642641
The acceptance radius for containment tests. See also
643642
`.Axis.contains`.
643+
clear : bool, default: True
644+
Whether to clear the Axis on creation.
644645
"""
645646
super().__init__()
646647
self._remove_overlapping_locs = True
@@ -674,7 +675,12 @@ def __init__(self, axes, *, pickradius=15):
674675
self._major_tick_kw = dict()
675676
self._minor_tick_kw = dict()
676677

677-
self.clear()
678+
if clear:
679+
self.clear()
680+
else:
681+
self.converter = None
682+
self.units = None
683+
678684
self._autoscale_on = True
679685

680686
@property

lib/matplotlib/axis.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ class Axis(martist.Artist):
105105
offsetText: Text
106106
labelpad: float
107107
pickradius: float
108-
def __init__(self, axes, *, pickradius: float = ...) -> None: ...
108+
def __init__(self, axes, *, pickradius: float = ...,
109+
clear: bool = ...) -> None: ...
109110
@property
110111
def isDefault_majloc(self) -> bool: ...
111112
@isDefault_majloc.setter

lib/matplotlib/projections/geo.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ def __call__(self, x, pos=None):
3030
RESOLUTION = 75
3131

3232
def _init_axis(self):
33-
self.xaxis = maxis.XAxis(self)
34-
self.yaxis = maxis.YAxis(self)
35-
# Do not register xaxis or yaxis with spines -- as done in
36-
# Axes._init_axis() -- until GeoAxes.xaxis.clear() works.
37-
# self.spines['geo'].register_axis(self.yaxis)
33+
self.xaxis = maxis.XAxis(self, clear=False)
34+
self.yaxis = maxis.YAxis(self, clear=False)
35+
self.spines['geo'].register_axis(self.yaxis)
3836

3937
def clear(self):
4038
# docstring inherited

lib/matplotlib/projections/polar.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -846,11 +846,10 @@ def clear(self):
846846

847847
def _init_axis(self):
848848
# This is moved out of __init__ because non-separable axes don't use it
849-
self.xaxis = ThetaAxis(self)
850-
self.yaxis = RadialAxis(self)
851-
# Calling polar_axes.xaxis.clear() or polar_axes.yaxis.clear()
852-
# results in weird artifacts. Therefore we disable this for now.
853-
# self.spines['polar'].register_axis(self.yaxis)
849+
self.xaxis = ThetaAxis(self, clear=False)
850+
self.yaxis = RadialAxis(self, clear=False)
851+
self.spines['inner'].register_axis(self.xaxis)
852+
self.spines['polar'].register_axis(self.yaxis)
854853

855854
def _set_lim_and_transforms(self):
856855
# A view limit where the minimum radius can be locked if the user

lib/matplotlib/spines.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,23 @@ def register_axis(self, axis):
214214
properties when needed.
215215
"""
216216
self.axis = axis
217-
if self.axis is not None:
218-
self.axis.clear()
219217
self.stale = True
220218

221219
def clear(self):
222220
"""Clear the current spine."""
223-
self._position = None # clear position
221+
self._clear()
224222
if self.axis is not None:
225223
self.axis.clear()
226224

225+
def _clear(self):
226+
"""
227+
Clear things directly related to the spine.
228+
229+
In this way it is possible to avoid clearing the Axis as well when calling
230+
from library code.
231+
"""
232+
self._position = None # clear position
233+
227234
def _adjust_location(self):
228235
"""Automatically set spine bounds to the view interval."""
229236

lib/matplotlib/text.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ def __init__(self,
136136
super().__init__()
137137
self._x, self._y = x, y
138138
self._text = ''
139-
self._antialiased = mpl.rcParams['text.antialiased']
140139
self._reset_visual_defaults(
141140
text=text,
142141
color=color,
@@ -191,8 +190,8 @@ def _reset_visual_defaults(
191190
linespacing = 1.2 # Maybe use rcParam later.
192191
self.set_linespacing(linespacing)
193192
self.set_rotation_mode(rotation_mode)
194-
if antialiased is not None:
195-
self.set_antialiased(antialiased)
193+
self.set_antialiased(antialiased if antialiased is not None else
194+
mpl.rcParams['text.antialiased'])
196195

197196
def update(self, kwargs):
198197
# docstring inherited

0 commit comments

Comments
 (0)