This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

Attachment 'mpl_editor.py'

Download

   1 """
   2 A simple demonstration of embedding a matplotlib plot window in 
   3 a traits-application. The CustomEditor allow any wxPython window
   4 to be used as an editor. The demo also illustrates Property traits,
   5 which provide nice dependency-handling and dynamic initialisation, using
   6 the _xxx_default(...) method.
   7 """
   8 
   9 from enthought.traits.api import HasTraits, Instance, Range,\
  10                                 Array, on_trait_change, Property,\
  11                                 cached_property, Bool
  12 from enthought.traits.ui.api import View, Item
  13 
  14 from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
  15 from matplotlib.backends.backend_wx import NavigationToolbar2Wx
  16 from matplotlib.figure import Figure
  17 from matplotlib.axes import Axes
  18 from matplotlib.lines import Line2D
  19 
  20 from enthought.traits.ui.api import CustomEditor
  21 import wx
  22 import numpy
  23 
  24 def MakePlot(parent, editor):
  25     """
  26     Builds the Canvas window for displaying the mpl-figure
  27     """
  28     fig = editor.object.figure
  29     panel = wx.Panel(parent, -1)
  30     canvas = FigureCanvasWxAgg(panel, -1, fig)
  31     toolbar = NavigationToolbar2Wx(canvas)
  32     toolbar.Realize()
  33     
  34     sizer = wx.BoxSizer(wx.VERTICAL)
  35     sizer.Add(canvas,1,wx.EXPAND|wx.ALL,1)
  36     sizer.Add(toolbar,0,wx.EXPAND|wx.ALL,1)
  37     panel.SetSizer(sizer)
  38     return panel
  39 
  40 
  41 class PlotModel(HasTraits):
  42     figure = Instance(Figure, ())
  43     axes = Instance(Axes)
  44     line = Instance(Line2D)
  45     
  46     _draw_pending = Bool(False)
  47     
  48     scale = Range(0.1,10.0)
  49     x = Array(value=numpy.linspace(-5,5,512))
  50     y = Property(Array, depends_on=['scale','x'])
  51     
  52     traits_view = View(
  53                     Item('figure', 
  54                          editor=CustomEditor(MakePlot),
  55                          resizable=True),
  56                     Item('scale'),
  57                     resizable=True
  58                     )
  59     
  60     def _axes_default(self):
  61         return self.figure.add_subplot(111)
  62     
  63     def _line_default(self):
  64         return self.axes.plot(self.x, self.y)[0]
  65     
  66     @cached_property
  67     def _get_y(self):
  68         return numpy.sin(self.scale * self.x)
  69     
  70     @on_trait_change("x, y")
  71     def update_line(self, obj, name, val):
  72         attr = {'x': "set_xdata", 'y': "set_ydata"}[name]
  73         getattr(self.line, attr)(val)
  74         self.redraw()
  75         
  76     def redraw(self):
  77         if self._draw_pending:
  78             return
  79         canvas = self.figure.canvas
  80         if canvas is None:
  81             return
  82         def _draw():
  83             canvas.draw()
  84             self._draw_pending = False
  85         wx.CallLater(50, _draw).Start()
  86         self._draw_pending = True
  87     
  88 if __name__=="__main__":
  89     model = PlotModel(scale=2.0)
  90     model.configure_traits()

New Attachment

File to upload
Rename to
Overwrite existing attachment of same name

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.