[Enthought-Dev] Embedding matplotlib in traits qt

Didrik Pinte dpinte at enthought.com
Tue May 29 06:33:44 CDT 2012


(forgot to CC the list)

> ---------- Forwarded message ----------
> From: Petrus Hyvönen <petrus.hyvonen at gmail.com>
> Date: Sat, May 26, 2012 at 7:38 AM
> Subject: [Enthought-Dev] Embedding matplotlib in traits qt
> To: enthought-dev at enthought.com
>
>
> Hi,
>
> I am trying to find some info about embedding matplotlib in a traits
> application, using qt. I am using the excellent basemap matplotlib map
> toolbox, so I can not use only chaco.
>
> There is an example using wx at
> http://gael-varoquaux.info/computers/traits_tutorial/traits_tutorial.html
> but I can find none about using qt.
>
> - Are there any examples or pointers of direction of embedding matplotlib in
> qt-traits?

>From Gael's example, you can do this (it works fine with PyQT but has
an issue with PySide):

from pyface.qt import QtGui, QtCore

import matplotlib
# We want matplotlib to use a QT backend
matplotlib.use('QtAgg')
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

from enthought.traits.api import Any, Instance
from enthought.traits.ui.qt4.editor import Editor
from enthought.traits.ui.qt4.basic_editor_factory import BasicEditorFactory

class _MPLFigureEditor(Editor):

   scrollable  = True

   def init(self, parent):
       self.control = self._create_canvas(parent)
       self.set_tooltip()

   def update_editor(self):
       pass

   def _create_canvas(self, parent):
       """ Create the MPL canvas. """
       # matplotlib commands to create a canvas
       mpl_canvas = FigureCanvas(self.value)
       return mpl_canvas

class MPLFigureEditor(BasicEditorFactory):

   klass = _MPLFigureEditor


if __name__ == "__main__":
   # Create a window to demo the editor
   from enthought.traits.api import HasTraits
   from enthought.traits.ui.api import View, Item
   from numpy import sin, cos, linspace, pi

   class Test(HasTraits):

       figure = Instance(Figure, ())

       view = View(Item('figure', editor=MPLFigureEditor(),
                               show_label=False),
                       width=400,
                       height=300,
                       resizable=True)

       def __init__(self):
           super(Test, self).__init__()
           axes = self.figure.add_subplot(111)
           t = linspace(0, 2*pi, 200)
           axes.plot(sin(t)*(1+0.5*cos(11*t)), cos(t)*(1+0.5*cos(11*t)))

   Test().configure_traits()


> - Or, are there any examples of using chaco as a front-end for basemap, or
> directly with some map-geometry libraries?

Work as started in enable to support more mapping features. You should
look at the different branches available there if you want. There is
no easy connection with map-geometry libraries yet (geos, shapely,
etc.).

-- Didrik


-- 
Didrik Pinte                                 +32 475 665 668
                                                   +44 1223 969515
Enthought Europe                      dpinte at enthought.com
Scientific Computing Solutions   http://www.enthought.com


More information about the Enthought-Dev mailing list