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

Skip to content

Commit efc0729

Browse files
authored
Merge pull request #11195 from anntzer/cleanups
Some unrelated cleanups.
2 parents 1652ed1 + 9ef6065 commit efc0729

File tree

6 files changed

+52
-131
lines changed

6 files changed

+52
-131
lines changed

lib/matplotlib/axes/_subplots.py

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import warnings
23

34
from matplotlib import docstring
@@ -87,18 +88,13 @@ def __init__(self, fig, *args, **kwargs):
8788
pos=True, subplot=True, artist=self)
8889

8990
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)
91+
# get the first axes class which does not inherit from a subplotbase
92+
axes_class = next(
93+
c for c in type(self).__mro__
94+
if issubclass(c, Axes) and not issubclass(c, SubplotBase))
95+
return (_picklable_subplot_class_constructor,
96+
(axes_class,),
97+
self.__getstate__())
10298

10399
def get_geometry(self):
104100
"""get the subplot geometry, e.g., 2,2,3"""
@@ -188,9 +184,8 @@ def _make_twin_axes(self, *kl, **kwargs):
188184
self._twinned_axes.join(self, ax2)
189185
return ax2
190186

191-
_subplot_classes = {}
192-
193187

188+
@functools.lru_cache(None)
194189
def subplot_class_factory(axes_class=None):
195190
# This makes a new class that inherits from SubplotBase and the
196191
# given axes_class (which is assumed to be a subclass of Axes).
@@ -199,62 +194,23 @@ def subplot_class_factory(axes_class=None):
199194
# not have to be created for every type of Axes.
200195
if axes_class is None:
201196
axes_class = Axes
197+
return type("%sSubplot" % axes_class.__name__,
198+
(SubplotBase, axes_class),
199+
{'_axes_class': axes_class})
202200

203-
new_class = _subplot_classes.get(axes_class)
204-
if new_class is None:
205-
new_class = type(str("%sSubplot") % (axes_class.__name__),
206-
(SubplotBase, axes_class),
207-
{'_axes_class': axes_class})
208-
_subplot_classes[axes_class] = new_class
209-
210-
return new_class
211201

212202
# This is provided for backward compatibility
213203
Subplot = subplot_class_factory()
214204

215205

216-
class _PicklableSubplotClassConstructor(object):
206+
def _picklable_subplot_class_constructor(axes_class):
217207
"""
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.
208+
This stub class exists to return the appropriate subplot class when called
209+
with an axes class. This is purely to allow pickling of Axes and Subplots.
221210
"""
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
211+
subplot_class = subplot_class_factory(axes_class)
212+
return subplot_class.__new__(subplot_class)
229213

230214

231215
docstring.interpd.update(Axes=martist.kwdoc(Axes))
232216
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,

lib/matplotlib/tests/test_axes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,7 @@ def __init__(self):
17791779
def _as_mpl_axes(self):
17801780
# implement the matplotlib axes interface
17811781
return PolarAxes, {'theta_offset': self.theta_offset}
1782+
17821783
prj = Polar()
17831784
prj2 = Polar()
17841785
prj2.theta_offset = np.pi
@@ -1793,7 +1794,7 @@ def _as_mpl_axes(self):
17931794

17941795
# testing axes creation with gca
17951796
ax = plt.gca(projection=prj)
1796-
assert type(ax) == maxes._subplots._subplot_classes[PolarAxes]
1797+
assert type(ax) == maxes._subplots.subplot_class_factory(PolarAxes)
17971798
ax_via_gca = plt.gca(projection=prj)
17981799
assert ax_via_gca is ax
17991800
# try getting the axes given a different polar projection
@@ -1814,7 +1815,7 @@ def _as_mpl_axes(self):
18141815

18151816
# testing axes creation with subplot
18161817
ax = plt.subplot(121, projection=prj)
1817-
assert type(ax) == maxes._subplots._subplot_classes[PolarAxes]
1818+
assert type(ax) == maxes._subplots.subplot_class_factory(PolarAxes)
18181819
plt.close()
18191820

18201821

lib/mpl_toolkits/axes_grid1/parasite_axes.py

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import six
1+
import functools
22

33
from matplotlib import (
44
artist as martist, collections as mcoll, transforms as mtransforms,
@@ -40,32 +40,21 @@ def cla(self):
4040
self.yaxis.set_zorder(2.5)
4141

4242

43-
_parasite_axes_classes = {}
43+
@functools.lru_cache(None)
4444
def parasite_axes_class_factory(axes_class=None):
4545
if axes_class is None:
4646
axes_class = Axes
4747

48-
new_class = _parasite_axes_classes.get(axes_class)
49-
if new_class is None:
50-
def _get_base_axes_attr(self, attrname):
51-
return getattr(axes_class, attrname)
48+
def _get_base_axes_attr(self, attrname):
49+
return getattr(axes_class, attrname)
5250

53-
new_class = type(str("%sParasite" % (axes_class.__name__)),
54-
(ParasiteAxesBase, axes_class),
55-
{'_get_base_axes_attr': _get_base_axes_attr})
56-
_parasite_axes_classes[axes_class] = new_class
51+
return type("%sParasite" % axes_class.__name__,
52+
(ParasiteAxesBase, axes_class),
53+
{'_get_base_axes_attr': _get_base_axes_attr})
5754

58-
return new_class
5955

6056
ParasiteAxes = parasite_axes_class_factory()
6157

62-
# #class ParasiteAxes(ParasiteAxesBase, Axes):
63-
64-
# @classmethod
65-
# def _get_base_axes_attr(cls, attrname):
66-
# return getattr(Axes, attrname)
67-
68-
6958

7059
class ParasiteAxesAuxTransBase(object):
7160
def __init__(self, parent_axes, aux_transform, viewlim_mode=None,
@@ -189,30 +178,22 @@ def apply_aspect(self, position=None):
189178
#ParasiteAxes.apply_aspect()
190179

191180

192-
193-
_parasite_axes_auxtrans_classes = {}
181+
@functools.lru_cache(None)
194182
def parasite_axes_auxtrans_class_factory(axes_class=None):
195183
if axes_class is None:
196184
parasite_axes_class = ParasiteAxes
197185
elif not issubclass(axes_class, ParasiteAxesBase):
198186
parasite_axes_class = parasite_axes_class_factory(axes_class)
199187
else:
200188
parasite_axes_class = axes_class
201-
202-
new_class = _parasite_axes_auxtrans_classes.get(parasite_axes_class)
203-
if new_class is None:
204-
new_class = type(str("%sParasiteAuxTrans" % (parasite_axes_class.__name__)),
205-
(ParasiteAxesAuxTransBase, parasite_axes_class),
206-
{'_parasite_axes_class': parasite_axes_class,
207-
'name': 'parasite_axes'})
208-
_parasite_axes_auxtrans_classes[parasite_axes_class] = new_class
209-
210-
return new_class
211-
212-
213-
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(axes_class=ParasiteAxes)
189+
return type("%sParasiteAuxTrans" % parasite_axes_class.__name__,
190+
(ParasiteAxesAuxTransBase, parasite_axes_class),
191+
{'_parasite_axes_class': parasite_axes_class,
192+
'name': 'parasite_axes'})
214193

215194

195+
ParasiteAxesAuxTrans = parasite_axes_auxtrans_class_factory(
196+
axes_class=ParasiteAxes)
216197

217198

218199
def _get_handles(ax):
@@ -391,33 +372,29 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
391372
return _bbox
392373

393374

394-
_host_axes_classes = {}
375+
@functools.lru_cache(None)
395376
def host_axes_class_factory(axes_class=None):
396377
if axes_class is None:
397378
axes_class = Axes
398379

399-
new_class = _host_axes_classes.get(axes_class)
400-
if new_class is None:
401-
def _get_base_axes(self):
402-
return axes_class
380+
def _get_base_axes(self):
381+
return axes_class
403382

404-
def _get_base_axes_attr(self, attrname):
405-
return getattr(axes_class, attrname)
383+
def _get_base_axes_attr(self, attrname):
384+
return getattr(axes_class, attrname)
406385

407-
new_class = type(str("%sHostAxes" % (axes_class.__name__)),
408-
(HostAxesBase, axes_class),
409-
{'_get_base_axes_attr': _get_base_axes_attr,
410-
'_get_base_axes': _get_base_axes})
386+
return type("%sHostAxes" % axes_class.__name__,
387+
(HostAxesBase, axes_class),
388+
{'_get_base_axes_attr': _get_base_axes_attr,
389+
'_get_base_axes': _get_base_axes})
411390

412-
_host_axes_classes[axes_class] = new_class
413-
414-
return new_class
415391

416392
def host_subplot_class_factory(axes_class):
417393
host_axes_class = host_axes_class_factory(axes_class=axes_class)
418394
subplot_host_class = subplot_class_factory(host_axes_class)
419395
return subplot_host_class
420396

397+
421398
HostAxes = host_axes_class_factory(axes_class=Axes)
422399
SubplotHost = subplot_class_factory(HostAxes)
423400

lib/mpl_toolkits/axisartist/floating_axes.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
22
An experimental support for curvilinear grid.
33
"""
4-
import six
5-
from six.moves import zip
4+
5+
import functools
66

77
# TODO :
88
# see if tick_iterator method can be simplified by reusing the parent method.
@@ -506,19 +506,12 @@ def adjust_axes_lim(self):
506506
self.set_ylim(ymin-dy, ymax+dy)
507507

508508

509-
510-
_floatingaxes_classes = {}
511-
509+
@functools.lru_cache(None)
512510
def floatingaxes_class_factory(axes_class):
511+
return type("Floating %s" % axes_class.__name__,
512+
(FloatingAxesBase, axes_class),
513+
{'_axes_class_floating': axes_class})
513514

514-
new_class = _floatingaxes_classes.get(axes_class)
515-
if new_class is None:
516-
new_class = type(str("Floating %s" % (axes_class.__name__)),
517-
(FloatingAxesBase, axes_class),
518-
{'_axes_class_floating': axes_class})
519-
_floatingaxes_classes[axes_class] = new_class
520-
521-
return new_class
522515

523516
from .axislines import Axes
524517
from mpl_toolkits.axes_grid1.parasite_axes import host_axes_class_factory

0 commit comments

Comments
 (0)