这有点晚了,但这是一个创建和破坏Chaco情节的例子.主界面PlotSelector定义了一些假数据和单选按钮,可以在两种不同的绘图样式(线条和条形图)之间切换.
此示例使用Traits事件来指示何时关闭绘图,然后使用PlotController.可能有更好的方法来关闭窗口,但我找不到一个.
编辑:更新了Traits,Chaco和Enable(ETS 4而不是3)版本的导入.
import numpy as np
import traits.api as traits
import traitsui.api as ui
import chaco.api as chaco
from enable.api import ComponentEditor
class PlotController(ui.Controller):
view = ui.View(ui.Item('plot', editor=ComponentEditor(), show_label=False),
height=300, width=300, resizable=True)
def object_close_signal_changed(self, info):
info.ui.dispose()
class BasicPlot(traits.HasTraits):
close_signal = traits.Event()
plot = traits.Instance(chaco.Plot)
class LinePlot(BasicPlot):
def __init__(self, plotdata):
self.plot = chaco.Plot(plotdata)
self.plot.plot(('x', 'y'))
class BarPlot(BasicPlot):
def __init__(self, plotdata):
self.plot = chaco.Plot(plotdata)
self.plot.candle_plot(('x', 'ymin', 'ymax'))
available_plot_types = dict(line=LinePlot, bar=BarPlot)
class PlotSelector(traits.HasTraits):
plot_type = traits.Enum(['line', 'bar'])
traits_view = ui.View('plot_type', style='custom')
def __init__(self, x, y):
ymin = y - 1
ymax = y + 1
self.plotdata = chaco.ArrayPlotData(x=x, y=y, ymin=ymin, ymax=ymax)
self.figure = None
def _plot_type_changed(self):
plot_class = available_plot_types[self.plot_type]
if self.figure is not None:
self.figure.close_signal = True
self.figure = plot_class(self.plotdata)
controller = PlotController(model=self.figure)
controller.edit_traits()
N = 20
x = np.arange(N)
y = x + np.random.normal(size=N)
plot_selector = PlotSelector(x, y)
plot_selector.configure_traits()
Run Code Online (Sandbox Code Playgroud)
请注意,主接口(PlotSelector)调用configure_traits(启动应用程序),同时查看绘图edit_traits(从应用程序内部调用).此外,请注意,这个例子调用edit_traits从PlotController而是从模型调用它.您可以改为将视图移动PlotController到BasicPlot并将该视图的处理程序方法设置为PlotController.
最后,如果您不需要完全破坏绘图窗口,那么您可能需要查看Plot对象的 delplot方法,该方法会破坏*sub*图(此处为线图或条形图).
我希望有所帮助.
| 归档时间: |
|
| 查看次数: |
2936 次 |
| 最近记录: |