Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make figure.py backend-agnostic

figure.py now uses pylab_setup() to probe for the backend module to set
up the figure canvas.
  • Loading branch information...
commit 8bd1d57d5c563c1e65a2f2eeb41e68b47953d541 1 parent 700b26b
@dmcdougall dmcdougall authored
Showing with 51 additions and 47 deletions.
  1. +2 −0  lib/matplotlib/backends/backend_agg.py
  2. +2 −0  lib/matplotlib/backends/backend_cairo.py
  3. +1 −1  lib/matplotlib/backends/backend_cocoaagg.py
  4. +2 −0  lib/matplotlib/backends/backend_emf.py
  5. +2 −0  lib/matplotlib/backends/backend_fltkagg.py
  6. +3 −1 lib/matplotlib/backends/backend_gdk.py
  7. +3 −1 lib/matplotlib/backends/backend_gtk.py
  8. +1 −1  lib/matplotlib/backends/backend_gtk3.py
  9. +1 −0  lib/matplotlib/backends/backend_gtk3agg.py
  10. +1 −0  lib/matplotlib/backends/backend_gtk3cairo.py
  11. +1 −0  lib/matplotlib/backends/backend_gtkagg.py
  12. +1 −0  lib/matplotlib/backends/backend_gtkcairo.py
  13. +2 −1  lib/matplotlib/backends/backend_macosx.py
  14. +2 −0  lib/matplotlib/backends/backend_pdf.py
  15. +2 −0  lib/matplotlib/backends/backend_pgf.py
  16. +2 −0  lib/matplotlib/backends/backend_ps.py
  17. +2 −0  lib/matplotlib/backends/backend_qt.py
  18. +2 −0  lib/matplotlib/backends/backend_qt4.py
  19. +2 −0  lib/matplotlib/backends/backend_qt4agg.py
  20. +2 −0  lib/matplotlib/backends/backend_qtagg.py
  21. +2 −0  lib/matplotlib/backends/backend_svg.py
  22. +2 −0  lib/matplotlib/backends/backend_template.py
  23. +2 −0  lib/matplotlib/backends/backend_tkagg.py
  24. +1 −0  lib/matplotlib/backends/backend_wx.py
  25. +2 −0  lib/matplotlib/backends/backend_wxagg.py
  26. +6 −42 lib/matplotlib/figure.py
View
2  lib/matplotlib/backends/backend_agg.py
@@ -515,3 +515,5 @@ def print_to_buffer(self):
(int(renderer.width), int(renderer.height)))
renderer.dpi = original_dpi
return result
+
+FigureCanvas = FigureCanvasAgg
View
2  lib/matplotlib/backends/backend_cairo.py
@@ -512,3 +512,5 @@ def _save (self, fo, format, **kwargs):
ctx.show_page()
surface.finish()
+
+FigureCanvas = FigureCanvasCairo
View
2  lib/matplotlib/backends/backend_cocoaagg.py
@@ -89,7 +89,7 @@ def stop_event_loop(self):
FigureCanvasBase.stop_event_loop_default(self)
stop_event_loop.__doc__=FigureCanvasBase.stop_event_loop_default.__doc__
-
+FigureCanvas = FigureCanvasCocoaAgg
NibClassBuilder.extractClasses('Matplotlib.nib', mplBundle)
View
2  lib/matplotlib/backends/backend_emf.py
@@ -727,6 +727,8 @@ def print_emf(self, filename, dpi=300, **kwargs):
def get_default_filetype(self):
return 'emf'
+FigureCanvas = FigureCanvasEMF
+
class FigureManagerEMF(FigureManagerBase):
"""
Wrap everything up into a window for the pylab interface
View
2  lib/matplotlib/backends/backend_fltkagg.py
@@ -231,6 +231,8 @@ def stop_event_loop(self):
FigureCanvasBase.stop_event_loop_default(self)
stop_event_loop.__doc__=FigureCanvasBase.stop_event_loop_default.__doc__
+FigureCanvas = FigureCanvasFltkAgg
+
def destroy_figure(ptr, figman):
figman.window.hide()
Fltk.Fl.wait(0) # This is needed to make the last figure vanish.
View
4 lib/matplotlib/backends/backend_gdk.py
@@ -434,7 +434,7 @@ def new_figure_manager_given_figure(num, figure):
return manager
-class FigureCanvasGDK (FigureCanvasBase):
+class FigureCanvasGDK(FigureCanvasBase):
def __init__(self, figure):
FigureCanvasBase.__init__(self, figure)
@@ -472,3 +472,5 @@ def _print_image(self, filename, format, *args, **kwargs):
0, 0, 0, 0, width, height)
pixbuf.save(filename, format)
+
+FigureCanvas = FigureCanvasGDK
View
4 lib/matplotlib/backends/backend_gtk.py
@@ -144,7 +144,7 @@ def _on_timer(self):
return False
-class FigureCanvasGTK (gtk.DrawingArea, FigureCanvasBase):
+class FigureCanvasGTK(gtk.DrawingArea, FigureCanvasBase):
keyvald = {65507 : 'control',
65505 : 'shift',
65513 : 'alt',
@@ -519,6 +519,8 @@ def stop_event_loop(self):
FigureCanvasBase.stop_event_loop_default(self)
stop_event_loop.__doc__=FigureCanvasBase.stop_event_loop_default.__doc__
+FigureCanvas = FigureCanvasGTK
+
class FigureManagerGTK(FigureManagerBase):
"""
Public attributes
View
2  lib/matplotlib/backends/backend_gtk3.py
@@ -98,7 +98,7 @@ def _on_timer(self):
self._timer = None
return False
-class FigureCanvasGTK3 (Gtk.DrawingArea, FigureCanvasBase):
+class FigureCanvasGTK3(Gtk.DrawingArea, FigureCanvasBase):
keyvald = {65507 : 'control',
65505 : 'shift',
65513 : 'alt',
View
1  lib/matplotlib/backends/backend_gtk3agg.py
@@ -67,6 +67,7 @@ def print_png(self, filename, *args, **kwargs):
agg = self.switch_backends(backend_agg.FigureCanvasAgg)
return agg.print_png(filename, *args, **kwargs)
+FigureCanvas = FigureCanvasGTK3Agg
class FigureManagerGTK3Agg(backend_gtk3.FigureManagerGTK3):
pass
View
1  lib/matplotlib/backends/backend_gtk3cairo.py
@@ -34,6 +34,7 @@ def on_draw_event(self, widget, ctx):
return False # finish event propagation?
+FigureCanvas = FigureCanvasGTK3Cairo
class FigureManagerGTK3Cairo(backend_gtk3.FigureManagerGTK3):
pass
View
1  lib/matplotlib/backends/backend_gtkagg.py
@@ -33,6 +33,7 @@ def _get_toolbar(self, canvas):
toolbar = None
return toolbar
+FigureCanvas = FigureManagerGTKAgg
def new_figure_manager(num, *args, **kwargs):
"""
View
1  lib/matplotlib/backends/backend_gtkcairo.py
@@ -55,6 +55,7 @@ def _renderer_init(self):
if _debug: print('%s.%s()' % (self.__class__.__name__, _fn_name()))
self._renderer = RendererGTKCairo (self.figure.dpi)
+FigureCanvas = FigureCanvasGTKCairo
class FigureManagerGTKCairo(FigureManagerGTK):
def _get_toolbar(self, canvas):
View
3  lib/matplotlib/backends/backend_macosx.py
@@ -268,7 +268,6 @@ class TimerMac(_macosx.Timer, TimerBase):
'''
# completely implemented at the C-level (in _macosx.Timer)
-
class FigureCanvasMac(_macosx.FigureCanvas, FigureCanvasBase):
"""
The canvas the figure renders into. Calls the draw and print fig
@@ -353,6 +352,8 @@ def new_timer(self, *args, **kwargs):
return TimerMac(*args, **kwargs)
+FigureCanvas = FigureCanvasMac
+
class FigureManagerMac(_macosx.FigureManager, FigureManagerBase):
"""
Wrap everything up into a window for the pylab interface
View
2  lib/matplotlib/backends/backend_pdf.py
@@ -2299,6 +2299,8 @@ def print_pdf(self, filename, **kwargs):
else: # we opened the file above; now finish it off
file.close()
+FigureCanvas = FigureCanvasPdf
+
class FigureManagerPdf(FigureManagerBase):
pass
View
2  lib/matplotlib/backends/backend_pgf.py
@@ -838,6 +838,8 @@ def _render_texts_pgf(self, fh):
def get_renderer(self):
return RendererPgf(self.figure, None)
+FigureCanvas = FigureCanvasPgf
+
class FigureManagerPgf(FigureManagerBase):
def __init__(self, *args):
FigureManagerBase.__init__(self, *args)
View
2  lib/matplotlib/backends/backend_ps.py
@@ -1357,6 +1357,8 @@ def write(self, *kl, **kwargs):
shutil.move(tmpfile, outfile)
os.chmod(outfile, mode)
+FigureCanvas = FigureCanvasPS
+
def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
paperWidth, paperHeight, orientation):
"""
View
2  lib/matplotlib/backends/backend_qt.py
@@ -203,6 +203,8 @@ def stop_event_loop(self):
FigureCanvasBase.stop_event_loop_default(self)
stop_event_loop.__doc__=FigureCanvasBase.stop_event_loop_default.__doc__
+FigureCanvas = FigureCanvasQT
+
class FigureManagerQT( FigureManagerBase ):
"""
Public attributes
View
2  lib/matplotlib/backends/backend_qt4.py
@@ -377,6 +377,8 @@ def idle_draw(*args):
self._idle = True
if d: QtCore.QTimer.singleShot(0, idle_draw)
+FigureCanvas = FigureCanvasQT
+
class FigureManagerQT( FigureManagerBase ):
"""
Public attributes
View
2  lib/matplotlib/backends/backend_qt4agg.py
@@ -145,3 +145,5 @@ def blit(self, bbox=None):
def print_figure(self, *args, **kwargs):
FigureCanvasAgg.print_figure(self, *args, **kwargs)
self.draw()
+
+FigureCanvas = FigureCanvasQTAgg
View
2  lib/matplotlib/backends/backend_qtagg.py
@@ -156,3 +156,5 @@ def blit(self, bbox=None):
def print_figure(self, *args, **kwargs):
FigureCanvasAgg.print_figure(self, *args, **kwargs)
self.draw()
+
+FigureCanvas = FigureCanvasQTAgg
View
2  lib/matplotlib/backends/backend_svg.py
@@ -1143,6 +1143,8 @@ def _print_svg(self, filename, svgwriter, fh_to_close=None, **kwargs):
def get_default_filetype(self):
return 'svg'
+FigureCanvas = FigureCanvasSVG
+
class FigureManagerSVG(FigureManagerBase):
pass
View
2  lib/matplotlib/backends/backend_template.py
@@ -245,6 +245,8 @@ def print_foo(self, filename, *args, **kwargs):
def get_default_filetype(self):
return 'foo'
+FigureCanvas = FigureCanvasTemplate
+
class FigureManagerTemplate(FigureManagerBase):
"""
Wrap everything up into a window for the pylab interface
View
2  lib/matplotlib/backends/backend_tkagg.py
@@ -505,6 +505,8 @@ def stop_event_loop(self):
FigureCanvasBase.stop_event_loop_default(self)
stop_event_loop.__doc__=FigureCanvasBase.stop_event_loop_default.__doc__
+FigureCanvas = FigureCanvasTkAgg
+
class FigureManagerTkAgg(FigureManagerBase):
"""
Public attributes
View
1  lib/matplotlib/backends/backend_wx.py
@@ -1400,6 +1400,7 @@ def _onEnter(self, evt):
"""Mouse has entered the window."""
FigureCanvasBase.enter_notify_event(self, guiEvent = evt)
+FigureCanvas = FigureCanvasWx
########################################################################
#
View
2  lib/matplotlib/backends/backend_wxagg.py
@@ -105,6 +105,8 @@ def print_figure(self, filename, *args, **kwargs):
if self._isDrawn:
self.draw()
+FigureCanvas = FigureCanvasWxAgg
+
class NavigationToolbar2WxAgg(NavigationToolbar2Wx):
def get_canvas(self, frame, fig):
return FigureCanvasWxAgg(frame, -1, fig)
View
48 lib/matplotlib/figure.py
@@ -318,7 +318,7 @@ def __init__(self,
self.patch.set_aa(False)
self._hold = rcParams['axes.hold']
- self.canvas = self._current_figure_canvas()
+ self.canvas = self._setup_canvas()
if subplotpars is None:
subplotpars = SubplotParams()
@@ -330,47 +330,11 @@ def __init__(self,
self.clf()
self._cachedRenderer = None
- def _current_figure_canvas(self):
- b = rcParams['backend'].lower()
- if b == 'agg':
- from backends.backend_agg import FigureCanvasAgg as FigureCanvas
- elif b == 'cairo':
- from backends.backend_cairo import FigureCanvasCairo as FigureCanvas
- elif b == 'cocoaag':
- from backends.backend_cocoaagg import FigureCanvasCocoaAgg as FigureCanvas
- elif b == 'fltkagg':
- from backends.backend_fltkagg import FigureCanvasFltkAgg as FigureCanvas
- elif b == 'gdk':
- from backends.backend_gdk import FigureCanvasGDK as FigureCanvas
- elif b == 'gtk':
- from backends.backend_gtk import FigureCanvasGTK as FigureCanvas
- elif b == 'gtkagg':
- from backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
- elif b == 'gtkcairo':
- from backends.backend_gtkcairo import FigureCanvasGTKCairo as FigureCanvas
- elif b == 'macosx':
- from backends.backend_macosx import FigureCanvasMac as FigureCanvas
- elif b == 'pdf':
- from backends.backend_pdf import FigureCanvasPdf as FigureCanvas
- elif b == 'ps':
- from backends.backend_ps import FigureCanvasPS as FigureCanvas
- elif b == 'qtagg':
- from backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas
- elif b == 'qt4agg':
- from backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
- elif b == 'svg':
- from backends.backend_svg import FigureCanvasSVG as FigureCanvas
- elif b == 'template':
- from backends.backend_template import FigureCanvasTemplate as FigureCanvas
- elif b == 'tkagg':
- from backends.backend_tkagg import FigureCanvasTkAgg as FigureCanvas
- elif b == 'wx':
- from backends.backend_wx import FigureCanvasWx as FigureCanvas
- elif b == 'wxagg':
- from backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
- else:
- return None
- return FigureCanvas(self)
+ def _setup_canvas(self):
+ # TODO: docstring
+ import matplotlib.backends as mbackends
+ backend_mod = mbackends.pylab_setup()[0]
+ return backend_mod.FigureCanvas(self)
@verbit
verbit added a note

What if the module from pylab_setup()[0] doesn't have a FigureCanvas class. Then the line return backend_mod.FigureCanvas(self) would result in an error. So it does with IPython that uses his own backend module but without a FigureCanvas class.

The old def _current_figure_canvas(self)works because in case it doesn't find the right FigureCanvas it returns None.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
def _get_axes(self):
return self._axstack.as_list()
@verbit

What if the module from pylab_setup()[0] doesn't have a FigureCanvas class. Then the line return backend_mod.FigureCanvas(self) would result in an error. So it does with IPython that uses his own backend module but without a FigureCanvas class.

The old def _current_figure_canvas(self)works because in case it doesn't find the right FigureCanvas it returns None.

Please sign in to comment.
Something went wrong with that request. Please try again.