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

Commitcf51167

Browse files
committed
Backend class to factor out common code.
1 parentd412228 commitcf51167

26 files changed

+470
-859
lines changed

‎lib/matplotlib/backend_bases.py

Lines changed: 103 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
frommatplotlibimportrcParams
5656
frommatplotlibimportis_interactive
5757
frommatplotlibimportget_backend
58-
frommatplotlib._pylab_helpersimportGcf
5958
frommatplotlibimportlines
59+
frommatplotlib._pylab_helpersimportGcf
6060

6161
frommatplotlib.transformsimportBbox,TransformedBbox,Affine2D
6262

@@ -141,60 +141,118 @@ def get_registered_canvas_class(format):
141141
returnbackend_class
142142

143143

144-
classShowBase(object):
145-
"""
146-
Simple base class to generate a show() callable in backends.
144+
class_Backend(object):
145+
# A backend can be defined by using the following pattern:
146+
#
147+
# @_Backend.export
148+
# class FooBackend(_Backend):
149+
# # override the attributes and methods documented below.
147150

148-
Subclass must override mainloop() method.
149-
"""
150-
def__call__(self,block=None):
151+
# The following attributes and methods must be overridden by subclasses.
152+
153+
# The `FigureCanvas` and `FigureManager` classes must be defined.
154+
FigureCanvas=None
155+
FigureManager=None
156+
157+
# The following methods must be left as None for non-interactive backends.
158+
# For interactive backends, `trigger_manager_draw` should be a function
159+
# taking a manager as argument and triggering a canvas draw, and `mainloop`
160+
# should be a function taking no argument and starting the backend main
161+
# loop.
162+
trigger_manager_draw=None
163+
mainloop=None
164+
165+
# The following methods will be automatically defined and exported, but
166+
# can be overridden.
167+
168+
@classmethod
169+
defnew_figure_manager(cls,num,*args,**kwargs):
170+
"""Create a new figure manager instance.
171+
"""
172+
# This import needs to happen here due to circular imports.
173+
frommatplotlib.figureimportFigure
174+
fig_cls=kwargs.pop('FigureClass',Figure)
175+
fig=fig_cls(*args,**kwargs)
176+
returncls.new_figure_manager_given_figure(num,fig)
177+
178+
@classmethod
179+
defnew_figure_manager_given_figure(cls,num,figure):
180+
"""Create a new figure manager instance for the given figure.
151181
"""
152-
Show all figures. If *block* is not None, then
153-
it is a boolean that overrides all other factors
154-
determining whether show blocks by calling mainloop().
155-
The other factors are:
156-
it does not block if run inside ipython's "%pylab" mode
157-
it does not block in interactive mode.
182+
canvas=cls.FigureCanvas(figure)
183+
manager=cls.FigureManager(canvas,num)
184+
returnmanager
185+
186+
@classmethod
187+
defdraw_if_interactive(cls):
188+
ifcls.trigger_manager_drawisnotNoneandis_interactive():
189+
manager=Gcf.get_active()
190+
ifmanager:
191+
cls.trigger_manager_draw(manager)
192+
193+
@classmethod
194+
defshow(cls,block=None):
195+
"""Show all figures.
196+
197+
`show` blocks by calling `mainloop` if *block* is ``True``, or if it
198+
is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in
199+
`interactive` mode.
158200
"""
201+
ifcls.mainloopisNone:
202+
return
159203
managers=Gcf.get_all_fig_managers()
160204
ifnotmanagers:
161205
return
162-
163206
formanagerinmanagers:
164207
manager.show()
208+
ifblockisNone:
209+
# Hack: Are we in IPython's pylab mode?
210+
frommatplotlibimportpyplot
211+
try:
212+
# IPython versions >= 0.10 tack the _needmain attribute onto
213+
# pyplot.show, and always set it to False, when in %pylab mode.
214+
ipython_pylab=notpyplot.show._needmain
215+
exceptAttributeError:
216+
ipython_pylab=False
217+
block=notipython_pylabandnotis_interactive()
218+
# TODO: The above is a hack to get the WebAgg backend working with
219+
# ipython's `%pylab` mode until proper integration is implemented.
220+
ifget_backend()=="WebAgg":
221+
block=True
222+
ifblock:
223+
cls.mainloop()
224+
225+
# This method is the one actually exporting the required methods.
226+
227+
@staticmethod
228+
defexport(cls):
229+
fornamein ["FigureCanvas",
230+
"FigureManager",
231+
"new_figure_manager",
232+
"new_figure_manager_given_figure",
233+
"draw_if_interactive",
234+
"show"]:
235+
setattr(sys.modules[cls.__module__],name,getattr(cls,name))
236+
237+
# For back-compatibility, generate a shim `Show` class.
238+
239+
classShow(ShowBase):
240+
defmainloop(self):
241+
returncls.mainloop()
242+
243+
setattr(sys.modules[cls.__module__],"Show",Show)
244+
returncls
245+
246+
247+
classShowBase(_Backend):
248+
"""
249+
Simple base class to generate a show() callable in backends.
165250
166-
ifblockisnotNone:
167-
ifblock:
168-
self.mainloop()
169-
return
170-
else:
171-
return
172-
173-
# Hack: determine at runtime whether we are
174-
# inside ipython in pylab mode.
175-
frommatplotlibimportpyplot
176-
try:
177-
ipython_pylab=notpyplot.show._needmain
178-
# IPython versions >= 0.10 tack the _needmain
179-
# attribute onto pyplot.show, and always set
180-
# it to False, when in %pylab mode.
181-
ipython_pylab=ipython_pylabandget_backend()!='WebAgg'
182-
# TODO: The above is a hack to get the WebAgg backend
183-
# working with ipython's `%pylab` mode until proper
184-
# integration is implemented.
185-
exceptAttributeError:
186-
ipython_pylab=False
187-
188-
# Leave the following as a separate step in case we
189-
# want to control this behavior with an rcParam.
190-
ifipython_pylab:
191-
return
192-
193-
ifnotis_interactive()orget_backend()=='WebAgg':
194-
self.mainloop()
251+
Subclass must override mainloop() method.
252+
"""
195253

196-
defmainloop(self):
197-
pass
254+
def__call__(self,block=None):
255+
returnself.show(block=block)
198256

199257

200258
classRendererBase(object):

‎lib/matplotlib/backends/backend_agg.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
fromcollectionsimportOrderedDict
3030
frommathimportradians,cos,sin
3131
frommatplotlibimportverbose,rcParams,__version__
32-
frommatplotlib.backend_basesimport (RendererBase,FigureManagerBase,
33-
FigureCanvasBase)
32+
frommatplotlib.backend_basesimport (
33+
_Backend,FigureCanvasBase,FigureManagerBase,RendererBase)
3434
frommatplotlib.cbookimportmaxdict,restrict_dict
3535
frommatplotlib.figureimportFigure
3636
frommatplotlib.font_managerimportfindfont,get_font
@@ -394,24 +394,6 @@ def post_processing(image, dpi):
394394
gc,l+ox,height-b-h+oy,img)
395395

396396

397-
defnew_figure_manager(num,*args,**kwargs):
398-
"""
399-
Create a new figure manager instance
400-
"""
401-
FigureClass=kwargs.pop('FigureClass',Figure)
402-
thisFig=FigureClass(*args,**kwargs)
403-
returnnew_figure_manager_given_figure(num,thisFig)
404-
405-
406-
defnew_figure_manager_given_figure(num,figure):
407-
"""
408-
Create a new figure manager instance for the given figure.
409-
"""
410-
canvas=FigureCanvasAgg(figure)
411-
manager=FigureManagerBase(canvas,num)
412-
returnmanager
413-
414-
415397
classFigureCanvasAgg(FigureCanvasBase):
416398
"""
417399
The canvas the figure renders into. Calls the draw and print fig
@@ -606,4 +588,7 @@ def print_tif(self, filename_or_obj, *args, **kwargs):
606588
print_tiff=print_tif
607589

608590

609-
FigureCanvas=FigureCanvasAgg
591+
@_Backend.export
592+
class_BackendAgg(_Backend):
593+
FigureCanvas=FigureCanvasAgg
594+
FigureManager=FigureManagerBase

‎lib/matplotlib/backends/backend_cairo.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@
5252
del_version_required
5353

5454
frommatplotlib.backend_basesimport (
55-
RendererBase,GraphicsContextBase,FigureManagerBase,FigureCanvasBase)
56-
frommatplotlib.figureimportFigure
57-
frommatplotlib.mathtextimportMathTextParser
58-
frommatplotlib.pathimportPath
59-
frommatplotlib.transformsimportBbox,Affine2D
55+
_Backend,FigureCanvasBase,FigureManagerBase,GraphicsContextBase,
56+
RendererBase)
57+
frommatplotlib.figureimportFigure
58+
frommatplotlib.mathtextimportMathTextParser
59+
frommatplotlib.pathimportPath
60+
frommatplotlib.transformsimportBbox,Affine2D
6061
frommatplotlib.font_managerimportttfFontProperty
6162

6263

@@ -452,24 +453,6 @@ def set_linewidth(self, w):
452453
self.ctx.set_line_width(self.renderer.points_to_pixels(w))
453454

454455

455-
defnew_figure_manager(num,*args,**kwargs):# called by backends/__init__.py
456-
"""
457-
Create a new figure manager instance
458-
"""
459-
FigureClass=kwargs.pop('FigureClass',Figure)
460-
thisFig=FigureClass(*args,**kwargs)
461-
returnnew_figure_manager_given_figure(num,thisFig)
462-
463-
464-
defnew_figure_manager_given_figure(num,figure):
465-
"""
466-
Create a new figure manager instance for the given figure.
467-
"""
468-
canvas=FigureCanvasCairo(figure)
469-
manager=FigureManagerBase(canvas,num)
470-
returnmanager
471-
472-
473456
classFigureCanvasCairo(FigureCanvasBase):
474457
defprint_png(self,fobj,*args,**kwargs):
475458
width,height=self.get_width_height()
@@ -555,4 +538,7 @@ def _save(self, fo, fmt, **kwargs):
555538
fo.close()
556539

557540

558-
FigureCanvas=FigureCanvasCairo
541+
@_Backend.export
542+
class_BackendCairo(_Backend):
543+
FigureCanvas=FigureCanvasCairo
544+
FigureManager=FigureManagerBase

‎lib/matplotlib/backends/backend_gdk.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
frommatplotlibimportrcParams
2525
frommatplotlib._pylab_helpersimportGcf
2626
frommatplotlib.backend_basesimport (
27-
RendererBase,GraphicsContextBase,FigureManagerBase,FigureCanvasBase)
27+
_Backend,FigureCanvasBase,FigureManagerBase,GraphicsContextBase,
28+
RendererBase)
2829
frommatplotlib.cbookimportrestrict_dict,warn_deprecated
2930
frommatplotlib.figureimportFigure
3031
frommatplotlib.mathtextimportMathTextParser
@@ -381,24 +382,6 @@ def set_linewidth(self, w):
381382
self.gdkGC.line_width=max(1,int(np.round(pixels)))
382383

383384

384-
defnew_figure_manager(num,*args,**kwargs):
385-
"""
386-
Create a new figure manager instance
387-
"""
388-
FigureClass=kwargs.pop('FigureClass',Figure)
389-
thisFig=FigureClass(*args,**kwargs)
390-
returnnew_figure_manager_given_figure(num,thisFig)
391-
392-
393-
defnew_figure_manager_given_figure(num,figure):
394-
"""
395-
Create a new figure manager instance for the given figure.
396-
"""
397-
canvas=FigureCanvasGDK(figure)
398-
manager=FigureManagerBase(canvas,num)
399-
returnmanager
400-
401-
402385
classFigureCanvasGDK (FigureCanvasBase):
403386
def__init__(self,figure):
404387
FigureCanvasBase.__init__(self,figure)
@@ -452,3 +435,9 @@ def _print_image(self, filename, format, *args, **kwargs):
452435
options['quality']=str(options['quality'])
453436

454437
pixbuf.save(filename,format,options=options)
438+
439+
440+
@_Backend.export
441+
class_BackendGDK(_Backend):
442+
FigureCanvas=FigureCanvasGDK
443+
FigureManager=FigureManagerBase

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp