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