Skip to content

Commit 62ff833

Browse files
committed
Replace axis_artist.AttributeCopier by normal inheritance.
Note that the "default" provided to get_attribute_from_ref_artist would previously never be triggered, as there is always a ref_artist present.
1 parent 146de7f commit 62ff833

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Deprecations
2+
````````````
3+
4+
For the `mpl_toolkits.axistartist.axis_artist.AttributeCopier` class, the
5+
constructor and the ``set_ref_artist`` method, and the *default_value*
6+
parameter of ``get_attribute_from_ref_artist``, are deprecated.
7+
8+
Deprecation of the constructor means that classes inheriting from
9+
`AttributeCopier` should no longer call its constructor.

lib/mpl_toolkits/axisartist/axis_artist.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
# angles are given in data coordinate - need to convert it to canvas coordinate
8787

8888

89+
from operator import methodcaller
90+
8991
import numpy as np
9092

9193
from matplotlib import cbook, rcParams
@@ -159,34 +161,31 @@ class UnimplementedException(Exception):
159161

160162

161163
class AttributeCopier:
164+
@cbook.deprecated("3.2")
162165
def __init__(self, ref_artist, klass=Artist):
163166
self._klass = klass
164167
self._ref_artist = ref_artist
165168
super().__init__()
166169

170+
@cbook.deprecated("3.2")
167171
def set_ref_artist(self, artist):
168172
self._ref_artist = artist
169173

170174
def get_ref_artist(self):
175+
"""
176+
Return the underlying artist that actually defines some properties
177+
(e.g., color) of this artist.
178+
"""
171179
raise RuntimeError("get_ref_artist must overridden")
172-
#return self._ref_artist
173-
174-
def get_attribute_from_ref_artist(self, attr_name, default_value):
175-
get_attr_method_name = "get_"+attr_name
176-
c = getattr(self._klass, get_attr_method_name)(self)
177-
if c == 'auto':
178-
ref_artist = self.get_ref_artist()
179-
if ref_artist:
180-
attr = getattr(ref_artist,
181-
get_attr_method_name)()
182-
return attr
183-
else:
184-
return default_value
185180

186-
return c
181+
@cbook._delete_parameter("3.2", "default_value")
182+
def get_attribute_from_ref_artist(self, attr_name, default_value=None):
183+
getter = methodcaller("get_" + attr_name)
184+
prop = getter(super())
185+
return getter(self.get_ref_artist()) if prop == "auto" else prop
187186

188187

189-
class Ticks(Line2D, AttributeCopier):
188+
class Ticks(AttributeCopier, Line2D):
190189
"""
191190
Ticks are derived from Line2D, and note that ticks themselves
192191
are markers. Thus, you should use set_mec, set_mew, etc.
@@ -211,23 +210,20 @@ def __init__(self, ticksize, tick_out=False, *, axis=None, **kwargs):
211210
kwargs["markeredgewidth"] = "auto"
212211

213212
Line2D.__init__(self, [0.], [0.], **kwargs)
214-
AttributeCopier.__init__(self, self._axis, klass=Line2D)
215213
self.set_snap(True)
216214

217215
def get_ref_artist(self):
218-
return self._ref_artist.majorTicks[0].tick1line
216+
# docstring inherited
217+
return self._axis.majorTicks[0].tick1line
219218

220219
def get_color(self):
221-
return self.get_attribute_from_ref_artist("color", "k")
220+
return self.get_attribute_from_ref_artist("color")
222221

223222
def get_markeredgecolor(self):
224-
if self._markeredgecolor == 'auto':
225-
return self.get_color()
226-
else:
227-
return self._markeredgecolor
223+
return self.get_attribute_from_ref_artist("markeredgecolor")
228224

229225
def get_markeredgewidth(self):
230-
return self.get_attribute_from_ref_artist("markeredgewidth", .5)
226+
return self.get_attribute_from_ref_artist("markeredgewidth")
231227

232228
def set_tick_out(self, b):
233229
"""
@@ -401,7 +397,7 @@ def get_window_extent(self, renderer):
401397
return bbox
402398

403399

404-
class AxisLabel(LabelBase, AttributeCopier):
400+
class AxisLabel(AttributeCopier, LabelBase):
405401
"""
406402
Axis Label. Derived from Text. The position of the text is updated
407403
in the fly, so changing text position has no effect. Otherwise, the
@@ -411,11 +407,8 @@ class AxisLabel(LabelBase, AttributeCopier):
411407
"""
412408

413409
def __init__(self, *args, axis_direction="bottom", axis=None, **kwargs):
414-
415410
self._axis = axis
416411
LabelBase.__init__(self, *args, **kwargs)
417-
AttributeCopier.__init__(self, self._axis, klass=LabelBase)
418-
419412
self.set_axis_direction(axis_direction)
420413
self._pad = 5
421414
self._extra_pad = 0
@@ -449,6 +442,7 @@ def _get_external_pad(self):
449442
return self._extra_pad
450443

451444
def get_ref_artist(self):
445+
# docstring inherited
452446
return self._axis.get_label()
453447

454448
def get_text(self):
@@ -501,7 +495,7 @@ def set_axis_direction(self, d):
501495
self.set_default_angle(d)
502496

503497
def get_color(self):
504-
return self.get_attribute_from_ref_artist("color", "k")
498+
return self.get_attribute_from_ref_artist("color")
505499

506500
def draw(self, renderer):
507501
if not self.get_visible():
@@ -526,7 +520,7 @@ def get_window_extent(self, renderer):
526520
return bb
527521

528522

529-
class TickLabels(AxisLabel, AttributeCopier): # mtext.Text
523+
class TickLabels(AxisLabel): # mtext.Text
530524
"""
531525
Tick Labels. While derived from Text, this single artist draws all
532526
ticklabels. As in AxisLabel, the position of the text is updated
@@ -543,8 +537,8 @@ def __init__(self, *, axis_direction="bottom", **kwargs):
543537
self.set_axis_direction(axis_direction)
544538
self._axislabel_pad = 0
545539

546-
# attribute copier
547540
def get_ref_artist(self):
541+
# docstring inherited
548542
return self._axis.get_ticklabels()[0]
549543

550544
def set_axis_direction(self, label_direction):

0 commit comments

Comments
 (0)