组合包含 matplotlib 图形的多个 TraitsUi 视图
Combining multiple TraitsUi Views containing matplotlib figures
我正在尝试使用 TraitsUI 构建交互式图像查看器。这可以使用 Pierre Haessig.
共享的实现来完成
接下来,我想同时展示其中的许多观点。概述了类似的方法 here。但是,我无法使用这种方法来解决手头的问题。构建这样一个容器会抛出 AttributeError: 'Test' object has no attribute 'dpi'
。我想我误解了编辑器在这种情况下的工作方式,但我不知道如何让它发挥作用。
可运行示例代码:
# By Pierre Haessig, https://gist.github.com/pierre-haessig/9838326
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()
##############
# This works #
##############
t = Test()
t.configure_traits()
class Container(HasTraits):
p1 = Instance(Test)
p2 = Instance(Test)
view = View(
Item("p1", editor=MPLFigureEditor()),
Item("p2", editor=MPLFigureEditor())
)
##############
# This fails #
##############
c = Container(p1=Test(), p2=Test())
c.configure_traits()
Test
class 本身不是 matplotlib 图,这就是为什么为 p1
和 p2
指定 editor=MPLFigureEditor()
会导致错误。如果您改为设置 style='custom'
,两个数字都会出现。
class Container(HasTraits):
p1 = Instance(Test)
p2 = Instance(Test)
view = View(
Item("p1", style='custom'),
Item("p2", style='custom')
)
我正在尝试使用 TraitsUI 构建交互式图像查看器。这可以使用 Pierre Haessig.
共享的实现来完成接下来,我想同时展示其中的许多观点。概述了类似的方法 here。但是,我无法使用这种方法来解决手头的问题。构建这样一个容器会抛出 AttributeError: 'Test' object has no attribute 'dpi'
。我想我误解了编辑器在这种情况下的工作方式,但我不知道如何让它发挥作用。
可运行示例代码:
# By Pierre Haessig, https://gist.github.com/pierre-haessig/9838326
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()
##############
# This works #
##############
t = Test()
t.configure_traits()
class Container(HasTraits):
p1 = Instance(Test)
p2 = Instance(Test)
view = View(
Item("p1", editor=MPLFigureEditor()),
Item("p2", editor=MPLFigureEditor())
)
##############
# This fails #
##############
c = Container(p1=Test(), p2=Test())
c.configure_traits()
Test
class 本身不是 matplotlib 图,这就是为什么为 p1
和 p2
指定 editor=MPLFigureEditor()
会导致错误。如果您改为设置 style='custom'
,两个数字都会出现。
class Container(HasTraits):
p1 = Instance(Test)
p2 = Instance(Test)
view = View(
Item("p1", style='custom'),
Item("p2", style='custom')
)