Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pierre-haessig
Last active January 18, 2019 22:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pierre-haessig/9838326 to your computer and use it in GitHub Desktop.
Save pierre-haessig/9838326 to your computer and use it in GitHub Desktop.
Matplotlib integration with Enthought's traitsui, using Qt toolkit (instead of wx)
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Pierre Haessig — March 2014
"""
Qt adaptation of Gael Varoquaux's tutorial to integrate Matplotlib
http://docs.enthought.com/traitsui/tutorials/traits_ui_scientific_app.html#extending-traitsui-adding-a-matplotlib-figure-to-our-application
based on Qt-based code shared by Didrik Pinte, May 2012
http://markmail.org/message/z3hnoqruk56g2bje
adapted and tested to work with PySide from Anaconda in March 2014
"""
from pyface.qt import QtGui, QtCore
import matplotlib
# We want matplotlib to use a QT backend
matplotlib.use('Qt4Agg')
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from traits.api import Any, Instance
from traitsui.qt4.editor import Editor
from traitsui.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 traits.api import HasTraits, Int, Float, on_trait_change
from traitsui.api import View, Item
from numpy import sin, cos, linspace, pi
class Test(HasTraits):
figure = Instance(Figure, ())
n = Int(11)
a = Float(0.5)
view = View(Item('figure', editor=MPLFigureEditor(),
show_label=False),
Item('n'),
Item('a'),
width=400,
height=300,
resizable=True)
def __init__(self):
super(Test, self).__init__()
axes = self.figure.add_subplot(111)
self._t = linspace(0, 2*pi, 200)
self.plot()
@on_trait_change('n,a')
def plot(self):
t = self._t
a = self.a
n = self.n
axes = self.figure.axes[0]
if not axes.lines:
axes.plot(sin(t)*(1+a*cos(n*t)), cos(t)*(1+a*cos(n*t)))
else:
l = axes.lines[0]
l.set_xdata(sin(t)*(1+a*cos(n*t)))
l.set_ydata(cos(t)*(1+a*cos(n*t)))
canvas = self.figure.canvas
if canvas is not None:
canvas.draw()
t = Test()
t.configure_traits()
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Pierre Haessig — March 2014
""" traitsui matplotlib editor with toolbar using qt backend
publicaly shared by Ryan Olf, April 2013
Source:
http://enthought-dev.117412.n3.nabble.com/traitsui-matplotlib-editor-with-toolbar-using-qt-backend-td4026437.html
"""
from pyface.qt import QtGui, QtCore
from traits.etsconfig.api import ETSConfig
ETSConfig.toolkit = 'qt4'
import matplotlib as mpl
mpl.rcParams['backend.qt4']='PySide'
# We want matplotlib to use a QT backend
mpl.use('Qt4Agg')
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg
from traits.api import Any, Instance
from traitsui.qt4.editor import Editor
from traitsui.qt4.basic_editor_factory import BasicEditorFactory
from traitsui.api import Handler
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
frame = QtGui.QWidget()
mpl_canvas = FigureCanvas(self.value)
mpl_canvas.setParent(frame)
mpl_toolbar = NavigationToolbar2QTAgg(mpl_canvas,frame)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(mpl_canvas)
vbox.addWidget(mpl_toolbar)
frame.setLayout(vbox)
return frame
class MPLFigureEditor(BasicEditorFactory):
klass = _MPLFigureEditor
class MPLInitHandler(Handler):
"""Handler calls mpl_setup() to initialize mpl events"""
def init(self, info):
"""This method gets called after the controls have all been
created but before they are displayed.
"""
info.object.mpl_setup()
return True
if __name__ == "__main__":
# Create a window to demo the editor
from traits.api import HasTraits
from traitsui.api import View, Item
from numpy import sin, cos, linspace, pi
from matplotlib.widgets import RectangleSelector
class Test(HasTraits):
figure = Instance(Figure, ())
view = View(Item('figure', editor=MPLFigureEditor(),
show_label=False),
handler=MPLInitHandler,
resizable=True)
def __init__(self):
super(Test, self).__init__()
self.axes = self.figure.add_subplot(111)
t = linspace(0, 2*pi, 200)
self.axes.plot(sin(t)*(1+0.5*cos(11*t)), cos(t)*(1+0.5*cos(11*t)))
def mpl_setup(self):
def onselect(eclick, erelease):
print "eclick: {}, erelease: {}".format(eclick,erelease)
self.rs = RectangleSelector(self.axes, onselect,
drawtype='box',useblit=True)
Test().configure_traits()
@bgriffen
Copy link

The above syntax is a tad out of date now. I made some modifications and create a boilerplate Python3+Matplotlib+Traits template here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment