Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a bit late, but here's an example that creates and destroys Chaco plots. The main interface is <code>PlotSelector</code>, which defines some fake data and radio buttons to switch between two different plot styles (line and bar plots).</p> <p>This example uses a Traits event to signal when to close a plot, and then handles that signal with <code>PlotController</code>. There may be a better way to close the window, but I couldn't find one.</p> <p><strong>Edit</strong>: Updated imports for newer versions of Traits, Chaco, and Enable (ETS 4 instead of 3).</p> <pre><code>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() </code></pre> <p>Note that the main interface (<code>PlotSelector</code>) calls <code>configure_traits</code> (starts application), while the plots are viewed with <code>edit_traits</code> (called from within application). Also, note that this example calls <code>edit_traits</code> from <code>PlotController</code> instead of calling it from the model. You could instead move the view from <code>PlotController</code> to <code>BasicPlot</code> and set the handler method of that view to <code>PlotController</code>.</p> <p>Finally, if you don't need to totally destroy the plot window, then you may want to look at the <code>Plot</code> object's <code>delplot</code> method, which destroys the *<em>sub</em>*plot (here the line plot or bar plot).</p> <p>I hope that helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload