Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8d1a031

Browse files
committed
Templatize class factories.
This makes mpl_toolkits axes classes picklable (see test_pickle) bygeneralizing the machinery of _picklable_subplot_class_constructor,which would otherwise have had to be reimplemented for each classfactory.
1 parent876415d commit8d1a031

File tree

7 files changed

+72
-103
lines changed

7 files changed

+72
-103
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The private ``matplotlib.axes._subplots._subplot_classes`` dict has been removed
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

‎lib/matplotlib/axes/_subplots.py

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
importfunctools
2-
31
frommatplotlibimport_api,docstring
42
importmatplotlib.artistasmartist
53
frommatplotlib.axes._axesimportAxes
@@ -37,15 +35,6 @@ def __init__(self, fig, *args, **kwargs):
3735
# This will also update the axes position.
3836
self.set_subplotspec(SubplotSpec._from_subplot_args(fig,args))
3937

40-
def__reduce__(self):
41-
# get the first axes class which does not inherit from a subplotbase
42-
axes_class=next(
43-
cforcintype(self).__mro__
44-
ifissubclass(c,Axes)andnotissubclass(c,SubplotBase))
45-
return (_picklable_subplot_class_constructor,
46-
(axes_class,),
47-
self.__getstate__())
48-
4938
@_api.deprecated(
5039
"3.4",alternative="get_subplotspec",
5140
addendum="(get_subplotspec returns a SubplotSpec instance.)")
@@ -170,59 +159,10 @@ def _make_twin_axes(self, *args, **kwargs):
170159
returntwin
171160

172161

173-
# this here to support cartopy which was using a private part of the
174-
# API to register their Axes subclasses.
175-
176-
# In 3.1 this should be changed to a dict subclass that warns on use
177-
# In 3.3 to a dict subclass that raises a useful exception on use
178-
# In 3.4 should be removed
179-
180-
# The slow timeline is to give cartopy enough time to get several
181-
# release out before we break them.
182-
_subplot_classes= {}
183-
184-
185-
@functools.lru_cache(None)
186-
defsubplot_class_factory(axes_class=None):
187-
"""
188-
Make a new class that inherits from `.SubplotBase` and the
189-
given axes_class (which is assumed to be a subclass of `.axes.Axes`).
190-
This is perhaps a little bit roundabout to make a new class on
191-
the fly like this, but it means that a new Subplot class does
192-
not have to be created for every type of Axes.
193-
"""
194-
ifaxes_classisNone:
195-
_api.warn_deprecated(
196-
"3.3",message="Support for passing None to subplot_class_factory "
197-
"is deprecated since %(since)s; explicitly pass the default Axes "
198-
"class instead. This will become an error %(removal)s.")
199-
axes_class=Axes
200-
try:
201-
# Avoid creating two different instances of GeoAxesSubplot...
202-
# Only a temporary backcompat fix. This should be removed in
203-
# 3.4
204-
returnnext(clsforclsinSubplotBase.__subclasses__()
205-
ifcls.__bases__== (SubplotBase,axes_class))
206-
exceptStopIteration:
207-
returntype("%sSubplot"%axes_class.__name__,
208-
(SubplotBase,axes_class),
209-
{'_axes_class':axes_class})
210-
211-
162+
subplot_class_factory=cbook._make_class_factory(
163+
SubplotBase,"{}Subplot","_axes_class",default_axes_class=Axes)
212164
Subplot=subplot_class_factory(Axes)# Provided for backward compatibility.
213165

214-
215-
def_picklable_subplot_class_constructor(axes_class):
216-
"""
217-
Stub factory that returns an empty instance of the appropriate subplot
218-
class when called with an axes class. This is purely to allow pickling of
219-
Axes and Subplots.
220-
"""
221-
subplot_class=subplot_class_factory(axes_class)
222-
returnsubplot_class.__new__(subplot_class)
223-
224-
225166
docstring.interpd.update(Axes_kwdoc=martist.kwdoc(Axes))
226-
docstring.dedent_interpd(Axes.__init__)
227-
228167
docstring.interpd.update(Subplot_kwdoc=martist.kwdoc(Axes))
168+
docstring.dedent_interpd(Axes.__init__)

‎lib/matplotlib/cbook/__init__.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,3 +2298,54 @@ def _unikey_or_keysym_to_mplkey(unikey, keysym):
22982298
"next":"pagedown",# Used by tk.
22992299
}.get(key,key)
23002300
returnkey
2301+
2302+
2303+
@functools.lru_cache(None)
2304+
def_make_class_factory(
2305+
mixin_class,fmt,axes_attr=None,*,default_axes_class=None):
2306+
"""
2307+
Return a function that creates picklable classes inheriting from a mixin.
2308+
2309+
After ::
2310+
2311+
factory = _make_class_factory(FooMixin, fmt, axes_attr)
2312+
FooAxes = factory(Axes)
2313+
2314+
``Foo`` is a class that inherits from ``FooMixin`` and ``Axes`` and **is
2315+
picklable** (picklability is what differentiates this from a plain call to
2316+
`type`). Its ``__name__`` is set to ``fmt.format(Axes.__name__)`` and the
2317+
base class is stored in the ``axes_attr`` attribute, if not None.
2318+
"""
2319+
2320+
@functools.lru_cache(None)
2321+
defclass_factory(axes_class):
2322+
# default_axes_class should go away once the deprecation elapses.
2323+
ifaxes_classisNoneanddefault_axes_classisnotNone:
2324+
warn_deprecated(
2325+
"3.3",message="Support for passing None to class factories "
2326+
"is deprecated since %(since)s and will be removed "
2327+
"%(removal)s; explicitly pass the default Axes class instead.")
2328+
returnclass_factory(default_axes_class)
2329+
d= {"__reduce__":
2330+
lambdaself: (_picklable_class_constructor,
2331+
(mixin_class,fmt,axes_attr,
2332+
default_axes_class,axes_class,),
2333+
self.__getstate__())}
2334+
ifaxes_attrisnotNone:
2335+
d[axes_attr]=axes_class
2336+
cls=type(
2337+
fmt.format(axes_class.__name__), (mixin_class,axes_class),d)
2338+
# Better in first approximation than __module__ = "matplotlib.cbook"...
2339+
cls.__module__=mixin_class.__module__
2340+
returncls
2341+
2342+
returnclass_factory
2343+
2344+
2345+
def_picklable_class_constructor(
2346+
base_cls,fmt,axes_attr,default_axes_class,axes_class):
2347+
"""Internal helper for _make_class_factory."""
2348+
cls=_make_class_factory(
2349+
base_cls,fmt,axes_attr,
2350+
default_axes_class=default_axes_class)(axes_class)
2351+
returncls.__new__(cls)

‎lib/matplotlib/tests/test_axes.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6325,21 +6325,6 @@ def test_spines_properbbox_after_zoom():
63256325
np.testing.assert_allclose(bb.get_points(),bb2.get_points(),rtol=1e-6)
63266326

63276327

6328-
deftest_cartopy_backcompat():
6329-
6330-
classDummy(matplotlib.axes.Axes):
6331-
...
6332-
6333-
classDummySubplot(matplotlib.axes.SubplotBase,Dummy):
6334-
_axes_class=Dummy
6335-
6336-
matplotlib.axes._subplots._subplot_classes[Dummy]=DummySubplot
6337-
6338-
FactoryDummySubplot=matplotlib.axes.subplot_class_factory(Dummy)
6339-
6340-
assertDummySubplotisFactoryDummySubplot
6341-
6342-
63436328
deftest_gettightbbox_ignore_nan():
63446329
fig,ax=plt.subplots()
63456330
remove_ticks_and_titles(fig)

‎lib/matplotlib/tests/test_pickle.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
importmatplotlib.pyplotasplt
1111
importmatplotlib.transformsasmtransforms
1212
importmatplotlib.figureasmfigure
13+
frommpl_toolkits.axes_grid1importparasite_axes
1314

1415

1516
deftest_simple():
@@ -212,3 +213,8 @@ def test_unpickle_canvas():
212213
out.seek(0)
213214
fig2=pickle.load(out)
214215
assertfig2.canvasisnotNone
216+
217+
218+
deftest_mpl_toolkits():
219+
ax=parasite_axes.host_axes([0,0,1,1])
220+
asserttype(pickle.loads(pickle.dumps(ax)))==parasite_axes.HostAxes

‎lib/mpl_toolkits/axes_grid1/parasite_axes.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,8 @@ def apply_aspect(self, position=None):
9494
# end of aux_transform support
9595

9696

97-
@functools.lru_cache(None)
98-
defparasite_axes_class_factory(axes_class):
99-
returntype("%sParasite"%axes_class.__name__,
100-
(ParasiteAxesBase,axes_class), {})
101-
102-
97+
parasite_axes_class_factory=cbook._make_class_factory(
98+
ParasiteAxesBase,"{}Parasite")
10399
ParasiteAxes=parasite_axes_class_factory(Axes)
104100

105101

@@ -283,7 +279,7 @@ def _add_twin_axes(self, axes_class, **kwargs):
283279
*kwargs* are forwarded to the parasite axes constructor.
284280
"""
285281
ifaxes_classisNone:
286-
axes_class=self._get_base_axes()
282+
axes_class=self._base_axes_class
287283
ax=parasite_axes_class_factory(axes_class)(self,**kwargs)
288284
self.parasites.append(ax)
289285
ax._remove_method=self._remove_any_twin
@@ -310,11 +306,10 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
310306
returnBbox.union([bforbinbbsifb.width!=0orb.height!=0])
311307

312308

313-
@functools.lru_cache(None)
314-
defhost_axes_class_factory(axes_class):
315-
returntype("%sHostAxes"%axes_class.__name__,
316-
(HostAxesBase,axes_class),
317-
{'_get_base_axes':lambdaself:axes_class})
309+
host_axes_class_factory=cbook._make_class_factory(
310+
HostAxesBase,"{}HostAxes","_base_axes_class")
311+
HostAxes=host_axes_class_factory(Axes)
312+
SubplotHost=subplot_class_factory(HostAxes)
318313

319314

320315
defhost_subplot_class_factory(axes_class):
@@ -323,10 +318,6 @@ def host_subplot_class_factory(axes_class):
323318
returnsubplot_host_class
324319

325320

326-
HostAxes=host_axes_class_factory(Axes)
327-
SubplotHost=subplot_class_factory(HostAxes)
328-
329-
330321
defhost_axes(*args,axes_class=Axes,figure=None,**kwargs):
331322
"""
332323
Create axes that can act as a hosts to parasitic axes.

‎lib/mpl_toolkits/axisartist/floating_axes.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
# TODO :
66
# see if tick_iterator method can be simplified by reusing the parent method.
77

8-
importfunctools
9-
108
importnumpyasnp
119

10+
frommatplotlibimportcbook
1211
importmatplotlib.patchesasmpatches
1312
frommatplotlib.pathimportPath
1413
importmatplotlib.axesasmaxes
@@ -360,13 +359,8 @@ def adjust_axes_lim(self):
360359
self.set_ylim(ymin-dy,ymax+dy)
361360

362361

363-
@functools.lru_cache(None)
364-
deffloatingaxes_class_factory(axes_class):
365-
returntype("Floating %s"%axes_class.__name__,
366-
(FloatingAxesBase,axes_class),
367-
{'_axes_class_floating':axes_class})
368-
369-
362+
floatingaxes_class_factory=cbook._make_class_factory(
363+
FloatingAxesBase,"Floating {}","_axes_class_floating")
370364
FloatingAxes=floatingaxes_class_factory(
371365
host_axes_class_factory(axislines.Axes))
372366
FloatingSubplot=maxes.subplot_class_factory(FloatingAxes)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp