|
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 |
|
@@ -3375,3 +3263,129 @@ def set_message(self, s): |
3375 | 3263 | Message text |
3376 | 3264 | """ |
3377 | 3265 | pass |
| 3266 | + |
| 3267 | + |
| 3268 | +class _Backend(object): |
| 3269 | + # A backend can be defined by using the following pattern: |
| 3270 | + # |
| 3271 | + # @_Backend.export |
| 3272 | + # class FooBackend(_Backend): |
| 3273 | + # # override the attributes and methods documented below. |
| 3274 | + |
| 3275 | + # May be overridden by the subclass. |
| 3276 | + backend_version = "unknown" |
| 3277 | + # The `FigureCanvas` class must be overridden. |
| 3278 | + FigureCanvas = None |
| 3279 | + # For interactive backends, the `FigureManager` class must be overridden. |
| 3280 | + FigureManager = FigureManagerBase |
| 3281 | + # The following methods must be left as None for non-interactive backends. |
| 3282 | + # For interactive backends, `trigger_manager_draw` should be a function |
| 3283 | + # taking a manager as argument and triggering a canvas draw, and `mainloop` |
| 3284 | + # should be a function taking no argument and starting the backend main |
| 3285 | + # loop. |
| 3286 | + trigger_manager_draw = None |
| 3287 | + mainloop = None |
| 3288 | + |
| 3289 | + # The following methods will be automatically defined and exported, but |
| 3290 | + # can be overridden. |
| 3291 | + |
| 3292 | + @classmethod |
| 3293 | + def new_figure_manager(cls, num, *args, **kwargs): |
| 3294 | + """Create a new figure manager instance. |
| 3295 | + """ |
| 3296 | + # This import needs to happen here due to circular imports. |
| 3297 | + from matplotlib.figure import Figure |
| 3298 | + fig_cls = kwargs.pop('FigureClass', Figure) |
| 3299 | + fig = fig_cls(*args, **kwargs) |
| 3300 | + return cls.new_figure_manager_given_figure(num, fig) |
| 3301 | + |
| 3302 | + @classmethod |
| 3303 | + def new_figure_manager_given_figure(cls, num, figure): |
| 3304 | + """Create a new figure manager instance for the given figure. |
| 3305 | + """ |
| 3306 | + canvas = cls.FigureCanvas(figure) |
| 3307 | + manager = cls.FigureManager(canvas, num) |
| 3308 | + return manager |
| 3309 | + |
| 3310 | + @classmethod |
| 3311 | + def draw_if_interactive(cls): |
| 3312 | + if cls.trigger_manager_draw is not None and is_interactive(): |
| 3313 | + manager = Gcf.get_active() |
| 3314 | + if manager: |
| 3315 | + cls.trigger_manager_draw(manager) |
| 3316 | + |
| 3317 | + @classmethod |
| 3318 | + def show(cls, block=None): |
| 3319 | + """Show all figures. |
| 3320 | +
|
| 3321 | + `show` blocks by calling `mainloop` if *block* is ``True``, or if it |
| 3322 | + is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in |
| 3323 | + `interactive` mode. |
| 3324 | + """ |
| 3325 | + if cls.mainloop is None: |
| 3326 | + frame = inspect.currentframe() |
| 3327 | + while frame: |
| 3328 | + if frame.f_code.co_filename in [ |
| 3329 | + "<stdin>", "<ipython console>"]: |
| 3330 | + warnings.warn("""\ |
| 3331 | +Your currently selected backend does not support show(). |
| 3332 | +Please select a GUI backend in your matplotlibrc file ('{}') |
| 3333 | +or with matplotlib.use()""".format(matplotlib.matplotlib_fname())) |
| 3334 | + break |
| 3335 | + else: |
| 3336 | + frame = frame.f_back |
| 3337 | + return |
| 3338 | + managers = Gcf.get_all_fig_managers() |
| 3339 | + if not managers: |
| 3340 | + return |
| 3341 | + for manager in managers: |
| 3342 | + manager.show() |
| 3343 | + if block is None: |
| 3344 | + # Hack: Are we in IPython's pylab mode? |
| 3345 | + from matplotlib import pyplot |
| 3346 | + try: |
| 3347 | + # IPython versions >= 0.10 tack the _needmain attribute onto |
| 3348 | + # pyplot.show, and always set it to False, when in %pylab mode. |
| 3349 | + ipython_pylab = not pyplot.show._needmain |
| 3350 | + except AttributeError: |
| 3351 | + ipython_pylab = False |
| 3352 | + block = not ipython_pylab and not is_interactive() |
| 3353 | + # TODO: The above is a hack to get the WebAgg backend working with |
| 3354 | + # ipython's `%pylab` mode until proper integration is implemented. |
| 3355 | + if get_backend() == "WebAgg": |
| 3356 | + block = True |
| 3357 | + if block: |
| 3358 | + cls.mainloop() |
| 3359 | + |
| 3360 | + # This method is the one actually exporting the required methods. |
| 3361 | + |
| 3362 | + @staticmethod |
| 3363 | + def export(cls): |
| 3364 | + for name in ["backend_version", |
| 3365 | + "FigureCanvas", |
| 3366 | + "FigureManager", |
| 3367 | + "new_figure_manager", |
| 3368 | + "new_figure_manager_given_figure", |
| 3369 | + "draw_if_interactive", |
| 3370 | + "show"]: |
| 3371 | + setattr(sys.modules[cls.__module__], name, getattr(cls, name)) |
| 3372 | + |
| 3373 | + # For back-compatibility, generate a shim `Show` class. |
| 3374 | + |
| 3375 | + class Show(ShowBase): |
| 3376 | + def mainloop(self): |
| 3377 | + return cls.mainloop() |
| 3378 | + |
| 3379 | + setattr(sys.modules[cls.__module__], "Show", Show) |
| 3380 | + return cls |
| 3381 | + |
| 3382 | + |
| 3383 | +class ShowBase(_Backend): |
| 3384 | + """ |
| 3385 | + Simple base class to generate a show() callable in backends. |
| 3386 | +
|
| 3387 | + Subclass must override mainloop() method. |
| 3388 | + """ |
| 3389 | + |
| 3390 | + def __call__(self, block=None): |
| 3391 | + return self.show(block=block) |
0 commit comments