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

Skip to content

Commit 4818c27

Browse files
committed
Some unrelated cleanups.
1) Simplify a bit the pickling setup for SubplotBase subclasses (basically, there was one layer of callable that was unnecessary). 2) Remove some commented out code that doesn't seem to achieve much, and has likely been commented out for years now. 3) No need to define `__internal_repr__` in mathtext and have `__repr__` call it; directly defining `__repr__` is sufficient. 4) The current definition of `Patch.__str__` has `str(Patch())` return `"Patch'>"`, which is clearly worse than even the default repr. I guess the intent was just to return `"Patch"`, but that's not the point of this PR; that can go in another PR if desired.
1 parent fb98a9a commit 4818c27

File tree

3 files changed

+14
-59
lines changed

3 files changed

+14
-59
lines changed

lib/matplotlib/axes/_subplots.py

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,13 @@ def __init__(self, fig, *args, **kwargs):
8787
pos=True, subplot=True, artist=self)
8888

8989
def __reduce__(self):
90-
# get the first axes class which does not
91-
# inherit from a subplotbase
92-
93-
def not_subplotbase(c):
94-
return issubclass(c, Axes) and not issubclass(c, SubplotBase)
95-
96-
axes_class = [c for c in self.__class__.mro()
97-
if not_subplotbase(c)][0]
98-
r = [_PicklableSubplotClassConstructor(),
99-
(axes_class,),
100-
self.__getstate__()]
101-
return tuple(r)
90+
# get the first axes class which does not inherit from a subplotbase
91+
axes_class = next(
92+
c for c in type(self).__mro__
93+
if issubclass(c, Axes) and not issubclass(c, SubplotBase))
94+
return (_picklable_subplot_class_constructor,
95+
(axes_class,),
96+
self.__getstate__())
10297

10398
def get_geometry(self):
10499
"""get the subplot geometry, e.g., 2,2,3"""
@@ -213,48 +208,14 @@ def subplot_class_factory(axes_class=None):
213208
Subplot = subplot_class_factory()
214209

215210

216-
class _PicklableSubplotClassConstructor(object):
211+
def _picklable_subplot_class_constructor(axes_class):
217212
"""
218-
This stub class exists to return the appropriate subplot
219-
class when __call__-ed with an axes class. This is purely to
220-
allow Pickling of Axes and Subplots.
213+
This stub class exists to return the appropriate subplot class when called
214+
with an axes class. This is purely to allow pickling of Axes and Subplots.
221215
"""
222-
def __call__(self, axes_class):
223-
# create a dummy object instance
224-
subplot_instance = _PicklableSubplotClassConstructor()
225-
subplot_class = subplot_class_factory(axes_class)
226-
# update the class to the desired subplot class
227-
subplot_instance.__class__ = subplot_class
228-
return subplot_instance
216+
subplot_class = subplot_class_factory(axes_class)
217+
return subplot_class.__new__(subplot_class)
229218

230219

231220
docstring.interpd.update(Axes=martist.kwdoc(Axes))
232221
docstring.interpd.update(Subplot=martist.kwdoc(Axes))
233-
234-
"""
235-
# this is some discarded code I was using to find the minimum positive
236-
# data point for some log scaling fixes. I realized there was a
237-
# cleaner way to do it, but am keeping this around as an example for
238-
# how to get the data out of the axes. Might want to make something
239-
# like this a method one day, or better yet make get_verts an Artist
240-
# method
241-
242-
minx, maxx = self.get_xlim()
243-
if minx<=0 or maxx<=0:
244-
# find the min pos value in the data
245-
xs = []
246-
for line in self.lines:
247-
xs.extend(line.get_xdata(orig=False))
248-
for patch in self.patches:
249-
xs.extend([x for x,y in patch.get_verts()])
250-
for collection in self.collections:
251-
xs.extend([x for x,y in collection.get_verts()])
252-
posx = [x for x in xs if x>0]
253-
if len(posx):
254-
255-
minx = min(posx)
256-
maxx = max(posx)
257-
# warning, probably breaks inverted axis
258-
self.set_xlim((0.1*minx, maxx))
259-
260-
"""

lib/matplotlib/mathtext.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,9 +1360,6 @@ def __init__(self):
13601360
self.size = 0
13611361

13621362
def __repr__(self):
1363-
return self.__internal_repr__()
1364-
1365-
def __internal_repr__(self):
13661363
return self.__class__.__name__
13671364

13681365
def get_kerning(self, next):
@@ -1449,7 +1446,7 @@ def __init__(self, c, state, math=True):
14491446
# pack phase, after we know the real fontsize
14501447
self._update_metrics()
14511448

1452-
def __internal_repr__(self):
1449+
def __repr__(self):
14531450
return '`%s`' % self.c
14541451

14551452
def _update_metrics(self):
@@ -1547,7 +1544,7 @@ def __init__(self, elements):
15471544

15481545
def __repr__(self):
15491546
return '[%s <%.02f %.02f %.02f %.02f> %s]' % (
1550-
self.__internal_repr__(),
1547+
super().__repr__(),
15511548
self.width, self.height,
15521549
self.depth, self.shift_amount,
15531550
' '.join([repr(x) for x in self.children]))

lib/matplotlib/patches.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ class Patch(artist.Artist):
3838
# subclass-by-subclass basis.
3939
_edge_default = False
4040

41-
def __str__(self):
42-
return str(self.__class__).split('.')[-1]
43-
4441
def __init__(self,
4542
edgecolor=None,
4643
facecolor=None,

0 commit comments

Comments
 (0)