Permalink
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...
1 parent 700b26b commit 8bd1d57d5c563c1e65a2f2eeb41e68b47953d541 @dmcdougall committed Sep 2, 2012
@@ -515,3 +515,5 @@ def print_to_buffer(self):
(int(renderer.width), int(renderer.height)))
renderer.dpi = original_dpi
return result
+
+FigureCanvas = FigureCanvasAgg
@@ -512,3 +512,5 @@ def _save (self, fo, format, **kwargs):
ctx.show_page()
surface.finish()
+
+FigureCanvas = FigureCanvasCairo
@@ -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)
@@ -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
@@ -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.
@@ -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
@@ -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
@@ -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',
@@ -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
@@ -34,6 +34,7 @@ def on_draw_event(self, widget, ctx):
return False # finish event propagation?
+FigureCanvas = FigureCanvasGTK3Cairo
class FigureManagerGTK3Cairo(backend_gtk3.FigureManagerGTK3):
pass
@@ -33,6 +33,7 @@ def _get_toolbar(self, canvas):
toolbar = None
return toolbar
+FigureCanvas = FigureManagerGTKAgg
def new_figure_manager(num, *args, **kwargs):
"""
@@ -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):
@@ -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
@@ -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
@@ -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)
@@ -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):
"""
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -1400,6 +1400,7 @@ def _onEnter(self, evt):
"""Mouse has entered the window."""
FigureCanvasBase.enter_notify_event(self, guiEvent = evt)
+FigureCanvas = FigureCanvasWx
########################################################################
#
@@ -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)
@@ -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)
def _get_axes(self):
return self._axstack.as_list()

0 comments on commit 8bd1d57

Please sign in to comment.