41
41
from contextlib import contextmanager
42
42
from functools import partial
43
43
import importlib
44
+ import inspect
44
45
import io
45
46
import os
46
47
import sys
@@ -126,120 +127,6 @@ def get_registered_canvas_class(format):
126
127
return backend_class
127
128
128
129
129
- class _Backend (object ):
130
- # A backend can be defined by using the following pattern:
131
- #
132
- # @_Backend.export
133
- # class FooBackend(_Backend):
134
- # # override the attributes and methods documented below.
135
-
136
- # The following attributes and methods must be overridden by subclasses.
137
-
138
- # The `FigureCanvas` and `FigureManager` classes must be defined.
139
- FigureCanvas = None
140
- FigureManager = None
141
-
142
- # The following methods must be left as None for non-interactive backends.
143
- # For interactive backends, `trigger_manager_draw` should be a function
144
- # taking a manager as argument and triggering a canvas draw, and `mainloop`
145
- # should be a function taking no argument and starting the backend main
146
- # loop.
147
- trigger_manager_draw = None
148
- mainloop = None
149
-
150
- # The following methods will be automatically defined and exported, but
151
- # can be overridden.
152
-
153
- @classmethod
154
- def new_figure_manager (cls , num , * args , ** kwargs ):
155
- """Create a new figure manager instance.
156
- """
157
- # This import needs to happen here due to circular imports.
158
- from matplotlib .figure import Figure
159
- fig_cls = kwargs .pop ('FigureClass' , Figure )
160
- fig = fig_cls (* args , ** kwargs )
161
- return cls .new_figure_manager_given_figure (num , fig )
162
-
163
- @classmethod
164
- def new_figure_manager_given_figure (cls , num , figure ):
165
- """Create a new figure manager instance for the given figure.
166
- """
167
- canvas = cls .FigureCanvas (figure )
168
- manager = cls .FigureManager (canvas , num )
169
- return manager
170
-
171
- @classmethod
172
- def draw_if_interactive (cls ):
173
- if cls .trigger_manager_draw is not None and is_interactive ():
174
- manager = Gcf .get_active ()
175
- if manager :
176
- cls .trigger_manager_draw (manager )
177
-
178
- @classmethod
179
- def show (cls , block = None ):
180
- """Show all figures.
181
-
182
- `show` blocks by calling `mainloop` if *block* is ``True``, or if it
183
- is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in
184
- `interactive` mode.
185
- """
186
- if cls .mainloop is None :
187
- return
188
- managers = Gcf .get_all_fig_managers ()
189
- if not managers :
190
- return
191
- for manager in managers :
192
- manager .show ()
193
- if block is None :
194
- # Hack: Are we in IPython's pylab mode?
195
- from matplotlib import pyplot
196
- try :
197
- # IPython versions >= 0.10 tack the _needmain attribute onto
198
- # pyplot.show, and always set it to False, when in %pylab mode.
199
- ipython_pylab = not pyplot .show ._needmain
200
- except AttributeError :
201
- ipython_pylab = False
202
- block = not ipython_pylab and not is_interactive ()
203
- # TODO: The above is a hack to get the WebAgg backend working with
204
- # ipython's `%pylab` mode until proper integration is implemented.
205
- if get_backend () == "WebAgg" :
206
- block = True
207
- if block :
208
- cls .mainloop ()
209
-
210
- # This method is the one actually exporting the required methods.
211
-
212
- @staticmethod
213
- def export (cls ):
214
- for name in ["FigureCanvas" ,
215
- "FigureManager" ,
216
- "new_figure_manager" ,
217
- "new_figure_manager_given_figure" ,
218
- "draw_if_interactive" ,
219
- "show" ]:
220
- setattr (sys .modules [cls .__module__ ], name , getattr (cls , name ))
221
-
222
- # For back-compatibility, generate a shim `Show` class.
223
-
224
- class Show (ShowBase ):
225
- def mainloop (self ):
226
- return cls .mainloop ()
227
-
228
- setattr (sys .modules [cls .__module__ ], "Show" , Show )
229
- return cls
230
-
231
-
232
- class ShowBase (_Backend ):
233
- """
234
- Simple base class to generate a show() callable in backends.
235
-
236
- Subclass must override mainloop() method.
237
- """
238
-
239
- def __call__ (self , block = None ):
240
- return self .show (block = block )
241
-
242
-
243
130
class RendererBase (object ):
244
131
"""An abstract base class to handle drawing/rendering operations.
245
132
@@ -3364,3 +3251,129 @@ def set_message(self, s):
3364
3251
Message text
3365
3252
"""
3366
3253
pass
3254
+
3255
+
3256
+ class _Backend (object ):
3257
+ # A backend can be defined by using the following pattern:
3258
+ #
3259
+ # @_Backend.export
3260
+ # class FooBackend(_Backend):
3261
+ # # override the attributes and methods documented below.
3262
+
3263
+ # May be overridden by the subclass.
3264
+ backend_version = "unknown"
3265
+ # The `FigureCanvas` class must be overridden.
3266
+ FigureCanvas = None
3267
+ # For interactive backends, the `FigureManager` class must be overridden.
3268
+ FigureManager = FigureManagerBase
3269
+ # The following methods must be left as None for non-interactive backends.
3270
+ # For interactive backends, `trigger_manager_draw` should be a function
3271
+ # taking a manager as argument and triggering a canvas draw, and `mainloop`
3272
+ # should be a function taking no argument and starting the backend main
3273
+ # loop.
3274
+ trigger_manager_draw = None
3275
+ mainloop = None
3276
+
3277
+ # The following methods will be automatically defined and exported, but
3278
+ # can be overridden.
3279
+
3280
+ @classmethod
3281
+ def new_figure_manager (cls , num , * args , ** kwargs ):
3282
+ """Create a new figure manager instance.
3283
+ """
3284
+ # This import needs to happen here due to circular imports.
3285
+ from matplotlib .figure import Figure
3286
+ fig_cls = kwargs .pop ('FigureClass' , Figure )
3287
+ fig = fig_cls (* args , ** kwargs )
3288
+ return cls .new_figure_manager_given_figure (num , fig )
3289
+
3290
+ @classmethod
3291
+ def new_figure_manager_given_figure (cls , num , figure ):
3292
+ """Create a new figure manager instance for the given figure.
3293
+ """
3294
+ canvas = cls .FigureCanvas (figure )
3295
+ manager = cls .FigureManager (canvas , num )
3296
+ return manager
3297
+
3298
+ @classmethod
3299
+ def draw_if_interactive (cls ):
3300
+ if cls .trigger_manager_draw is not None and is_interactive ():
3301
+ manager = Gcf .get_active ()
3302
+ if manager :
3303
+ cls .trigger_manager_draw (manager )
3304
+
3305
+ @classmethod
3306
+ def show (cls , block = None ):
3307
+ """Show all figures.
3308
+
3309
+ `show` blocks by calling `mainloop` if *block* is ``True``, or if it
3310
+ is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in
3311
+ `interactive` mode.
3312
+ """
3313
+ if cls .mainloop is None :
3314
+ frame = inspect .currentframe ()
3315
+ while frame :
3316
+ if frame .f_code .co_filename in [
3317
+ "<stdin>" , "<ipython console>" ]:
3318
+ warnings .warn ("""\
3319
+ Your currently selected backend does not support show().
3320
+ Please select a GUI backend in your matplotlibrc file ('{}')
3321
+ or with matplotlib.use()""" .format (matplotlib .matplotlib_fname ()))
3322
+ break
3323
+ else :
3324
+ frame = frame .f_back
3325
+ return
3326
+ managers = Gcf .get_all_fig_managers ()
3327
+ if not managers :
3328
+ return
3329
+ for manager in managers :
3330
+ manager .show ()
3331
+ if block is None :
3332
+ # Hack: Are we in IPython's pylab mode?
3333
+ from matplotlib import pyplot
3334
+ try :
3335
+ # IPython versions >= 0.10 tack the _needmain attribute onto
3336
+ # pyplot.show, and always set it to False, when in %pylab mode.
3337
+ ipython_pylab = not pyplot .show ._needmain
3338
+ except AttributeError :
3339
+ ipython_pylab = False
3340
+ block = not ipython_pylab and not is_interactive ()
3341
+ # TODO: The above is a hack to get the WebAgg backend working with
3342
+ # ipython's `%pylab` mode until proper integration is implemented.
3343
+ if get_backend () == "WebAgg" :
3344
+ block = True
3345
+ if block :
3346
+ cls .mainloop ()
3347
+
3348
+ # This method is the one actually exporting the required methods.
3349
+
3350
+ @staticmethod
3351
+ def export (cls ):
3352
+ for name in ["backend_version" ,
3353
+ "FigureCanvas" ,
3354
+ "FigureManager" ,
3355
+ "new_figure_manager" ,
3356
+ "new_figure_manager_given_figure" ,
3357
+ "draw_if_interactive" ,
3358
+ "show" ]:
3359
+ setattr (sys .modules [cls .__module__ ], name , getattr (cls , name ))
3360
+
3361
+ # For back-compatibility, generate a shim `Show` class.
3362
+
3363
+ class Show (ShowBase ):
3364
+ def mainloop (self ):
3365
+ return cls .mainloop ()
3366
+
3367
+ setattr (sys .modules [cls .__module__ ], "Show" , Show )
3368
+ return cls
3369
+
3370
+
3371
+ class ShowBase (_Backend ):
3372
+ """
3373
+ Simple base class to generate a show() callable in backends.
3374
+
3375
+ Subclass must override mainloop() method.
3376
+ """
3377
+
3378
+ def __call__ (self , block = None ):
3379
+ return self .show (block = block )
0 commit comments