diff --git a/examples/demo/advanced/data_cube.py b/examples/demo/advanced/data_cube.py index d8419575f..396e7af04 100644 --- a/examples/demo/advanced/data_cube.py +++ b/examples/demo/advanced/data_cube.py @@ -26,13 +26,12 @@ ArrayPlotData, Plot, GridPlotContainer, - BaseTool, DataRange1D, ) from chaco.default_colormaps import * from chaco.tools.api import LineInspector, ZoomTool from enable.example_support import DemoFrame, demo_main -from enable.api import Window +from enable.api import BaseTool, Window from traits.api import ( Any, Array, diff --git a/examples/demo/aspect_ratio.py b/examples/demo/aspect_ratio.py index 0539398e9..d1ed2c35d 100644 --- a/examples/demo/aspect_ratio.py +++ b/examples/demo/aspect_ratio.py @@ -68,8 +68,7 @@ class MyPlot(HasTraits): title="Aspect Ratio Example", ) - def __init__(self, *args, **kw): - HasTraits.__init__(self, *args, **kw) + def _plot_default(self): numpoints = 200 plotdata = ArrayPlotData( x=sort(random(numpoints)), y=random(numpoints) @@ -78,7 +77,7 @@ def __init__(self, *args, **kw): plot.plot(("x", "y"), type="scatter") plot.tools.append(PanTool(plot)) plot.overlays.append(ZoomTool(plot)) - self.plot = plot + return plot def _screen_enabled_changed(self): if self.screen_enabled: diff --git a/examples/demo/basic/bar_plot_stacked.py b/examples/demo/basic/bar_plot_stacked.py index 8105d7ced..bca92cf78 100644 --- a/examples/demo/basic/bar_plot_stacked.py +++ b/examples/demo/basic/bar_plot_stacked.py @@ -15,17 +15,12 @@ class PlotExample(HasTraits): - plot = Instance(Plot) - traits_view = View( - UItem("plot", editor=ComponentEditor()), - width=400, - height=400, - resizable=True, - ) - def __init__(self, index, series_a, series_b, series_c, **kw): - super(PlotExample, self).__init__(**kw) + plot = Instance(Plot) + def _plot_default(self): + index = numpy.array([1, 2, 3, 4, 5]) + series_a, series_b, series_c = (index * 10, index * 5, index * 2) # Stack them up series_c = series_c + series_b + series_a series_b = series_b + series_a @@ -34,18 +29,18 @@ def __init__(self, index, series_a, series_b, series_c, **kw): plot_data.set_data("series_a", series_a) plot_data.set_data("series_b", series_b) plot_data.set_data("series_c", series_c) - self.plot = Plot(plot_data) - self.plot.plot( + plot = Plot(plot_data) + plot.plot( ("index", "series_a"), type="bar", bar_width=0.8, color="auto" ) - self.plot.plot( + plot.plot( ("index", "series_b"), type="bar", bar_width=0.8, color="auto", starting_value=ArrayDataSource(series_a), ) - self.plot.plot( + plot.plot( ("index", "series_c"), type="bar", bar_width=0.8, @@ -54,11 +49,11 @@ def __init__(self, index, series_a, series_b, series_c, **kw): ) # set the plot's value range to 0, otherwise it may pad too much - self.plot.value_range.low = 0 + plot.value_range.low = 0 # replace the index values with some nicer labels label_axis = LabelAxis( - self.plot, + plot, orientation="bottom", title="Months", positions=list(range(1, 10)), @@ -66,13 +61,21 @@ def __init__(self, index, series_a, series_b, series_c, **kw): small_haxis_style=True, ) - self.plot.underlays.remove(self.plot.index_axis) - self.plot.index_axis = label_axis - self.plot.underlays.append(label_axis) + plot.underlays.remove(plot.index_axis) + plot.index_axis = label_axis + plot.underlays.append(label_axis) + + return plot + + traits_view = View( + UItem("plot", editor=ComponentEditor()), + width=400, + height=400, + resizable=True, + ) -index = numpy.array([1, 2, 3, 4, 5]) -demo = PlotExample(index, index * 10, index * 5, index * 2) +demo = PlotExample() if __name__ == "__main__": demo.configure_traits() diff --git a/examples/demo/depth.py b/examples/demo/depth.py index 1bda79dee..5a324a713 100644 --- a/examples/demo/depth.py +++ b/examples/demo/depth.py @@ -24,21 +24,20 @@ class MyPlot(HasTraits): resizable=True, ) - def __init__(self, depth, data_series, **kw): - super(MyPlot, self).__init__(**kw) - + def _plot_default(self): + depth = numpy.arange(1.0, 100.0, 0.1) + data_series = numpy.sin(depth) + depth / 10.0 plot_data = ArrayPlotData(index=depth) plot_data.set_data("data_series", data_series) - self.plot = ToolbarPlot(plot_data, orientation="v", origin="top left") - line = self.plot.plot(("index", "data_series"))[0] + plot = ToolbarPlot(plot_data, orientation="v", origin="top left") + line = plot.plot(("index", "data_series"))[0] line_inspector = LineInspector(component=line, write_metadata=True) line.tools.append(line_inspector) line.overlays.append(line_inspector) + return plot -depth = numpy.arange(1.0, 100.0, 0.1) -data_series = numpy.sin(depth) + depth / 10.0 -my_plot = MyPlot(depth, data_series) +my_plot = MyPlot() my_plot.configure_traits() diff --git a/examples/demo/domain_limits.py b/examples/demo/domain_limits.py index 39d1fded6..1d85f4e7c 100644 --- a/examples/demo/domain_limits.py +++ b/examples/demo/domain_limits.py @@ -17,25 +17,17 @@ class ExamplePlotApp(HasTraits): plot = Instance(Plot) - traits_view = View( - Item( - "plot", - editor=ComponentEditor(), - width=600, - height=600, - show_label=False, - ), - resizable=True, - ) + def _plot_default(self): + index = numpy.arange(1.0, 10.0, 0.01) + series1 = (100.0 + index) / (100.0 - 20 * index ** 2 + 5.0 * index**4) + series2 = (100.0 + index) / (100.0 - 20 * index ** 2 + 5.0 * index**3) - def __init__(self, index, series1, series2, **kw): - super(ExamplePlotApp, self).__init__(**kw) plot_data = ArrayPlotData(index=index) plot_data.set_data("series1", series1) plot_data.set_data("series2", series2) - self.plot = ToolbarPlot(plot_data) - line_plot = self.plot.plot(("index", "series1"), color="auto")[0] + plot = ToolbarPlot(plot_data) + line_plot = plot.plot(("index", "series1"), color="auto")[0] # Add pan and zoom tools line_plot.tools.append(PanTool(line_plot)) @@ -44,11 +36,21 @@ def __init__(self, index, series1, series2, **kw): # Set the domain_limits line_plot.index_mapper.domain_limits = (3.3, 6.6) + return plot + + traits_view = View( + Item( + "plot", + editor=ComponentEditor(), + width=600, + height=600, + show_label=False, + ), + resizable=True, + ) + -index = numpy.arange(1.0, 10.0, 0.01) -series1 = (100.0 + index) / (100.0 - 20 * index ** 2 + 5.0 * index ** 4) -series2 = (100.0 + index) / (100.0 - 20 * index ** 2 + 5.0 * index ** 3) -demo = ExamplePlotApp(index, series1, series2) +demo = ExamplePlotApp() if __name__ == "__main__": demo.configure_traits() diff --git a/examples/demo/multi_line_plot.py b/examples/demo/multi_line_plot.py index 6a7d06906..959c80238 100644 --- a/examples/demo/multi_line_plot.py +++ b/examples/demo/multi_line_plot.py @@ -19,15 +19,13 @@ class MyPlot(HasTraits): plot = Instance(Plot) - traits_view = View( - UItem("plot", editor=ComponentEditor()), - width=700, - height=600, - resizable=True, - ) - - def __init__(self, x_index, y_index, data, **kw): - super(MyPlot, self).__init__(**kw) + def _plot_default(self): + x_index = np.arange(0, 100, 1) + y_index = np.arange(0, 1000, 10) + data = np.sin(np.arange(0, x_index.size * y_index.size)) + # add a random chunk of nan values + data[1532:1588] = np.nan + data = data.reshape(x_index.size, y_index.size) # Create the data source for the MultiLinePlot. ds = MultiArrayDataSource(data=data) @@ -48,19 +46,20 @@ def __init__(self, x_index, y_index, data, **kw): value=ds, global_max=np.nanmax(data), global_min=np.nanmin(data), - **kw ) - self.plot = Plot() - self.plot.add(mlp) + plot = Plot() + plot.add(mlp) + return plot + + traits_view = View( + UItem("plot", editor=ComponentEditor()), + width=700, + height=600, + resizable=True, + ) -x_index = np.arange(0, 100, 1) -y_index = np.arange(0, 1000, 10) -data = np.sin(np.arange(0, x_index.size * y_index.size)) -# add a random chunk of nan values -data[1532:1588] = np.nan -data = data.reshape(x_index.size, y_index.size) -my_plot = MyPlot(x_index, y_index, data) +my_plot = MyPlot() my_plot.configure_traits() diff --git a/examples/demo/status_overlay.py b/examples/demo/status_overlay.py index 6c62b0826..2deb757cd 100644 --- a/examples/demo/status_overlay.py +++ b/examples/demo/status_overlay.py @@ -22,25 +22,16 @@ class MyPlot(HasTraits): warn_button = Button("Warning") no_problem_button = Button("No problem") - traits_view = View( - HGroup( - UItem("error_button"), - UItem("warn_button"), - UItem("no_problem_button"), - ), - UItem("plot", editor=ComponentEditor()), - width=700, - height=600, - resizable=True, - ) - - def __init__(self, index, data_series, **kw): - super(MyPlot, self).__init__(**kw) + def _plot_default(self): + index = numpy.array([1, 2, 3, 4, 5]) + data_series = index ** 2 plot_data = ArrayPlotData(index=index) plot_data.set_data("data_series", data_series) - self.plot = Plot(plot_data) - self.plot.plot(("index", "data_series")) + plot = Plot(plot_data) + plot.plot(("index", "data_series")) + + return plot def _error_button_fired(self, event): """removes the old overlay and replaces it with @@ -78,9 +69,18 @@ def clear_status(self): # fade_out will remove the overlay when its done self.status_overlay.fade_out() + traits_view = View( + HGroup( + UItem("error_button"), + UItem("warn_button"), + UItem("no_problem_button"), + ), + UItem("plot", editor=ComponentEditor()), + width=700, + height=600, + resizable=True, + ) -index = numpy.array([1, 2, 3, 4, 5]) -data_series = index ** 2 -my_plot = MyPlot(index, data_series) +my_plot = MyPlot() my_plot.configure_traits() diff --git a/examples/demo/toolbar_plot.py b/examples/demo/toolbar_plot.py index 7c47753f5..950c978ec 100644 --- a/examples/demo/toolbar_plot.py +++ b/examples/demo/toolbar_plot.py @@ -23,6 +23,21 @@ class ExamplePlotApp(HasTraits): plot = Instance(Plot) + def _plot_default(self): + index = numpy.arange(1.0, 10.0, 0.01) + series1 = (100.0 + index) / (100.0 - 20 * index ** 2 + 5.0 * index**4) + series2 = (100.0 + index) / (100.0 - 20 * index ** 2 + 5.0 * index**3) + + plot_data = ArrayPlotData(index=index) + plot_data.set_data("series1", series1) + plot_data.set_data("series2", series2) + + plot = ToolbarPlot(plot_data) + plot.plot(("index", "series1"), color="auto") + plot.plot(("index", "series2"), color="auto") + + return plot + traits_view = View( Item( "plot", @@ -34,21 +49,8 @@ class ExamplePlotApp(HasTraits): resizable=True, ) - def __init__(self, index, series1, series2, **kw): - super(ExamplePlotApp, self).__init__(**kw) - plot_data = ArrayPlotData(index=index) - plot_data.set_data("series1", series1) - plot_data.set_data("series2", series2) - - self.plot = ToolbarPlot(plot_data) - self.plot.plot(("index", "series1"), color="auto") - self.plot.plot(("index", "series2"), color="auto") - -index = numpy.arange(1.0, 10.0, 0.01) -series1 = (100.0 + index) / (100.0 - 20 * index ** 2 + 5.0 * index ** 4) -series2 = (100.0 + index) / (100.0 - 20 * index ** 2 + 5.0 * index ** 3) -demo = ExamplePlotApp(index, series1, series2) +demo = ExamplePlotApp() if __name__ == "__main__": demo.configure_traits() diff --git a/examples/demo/xray_plot.py b/examples/demo/xray_plot.py index 23e58b306..f606614c0 100644 --- a/examples/demo/xray_plot.py +++ b/examples/demo/xray_plot.py @@ -152,25 +152,25 @@ class PlotExample(HasTraits): plot = Instance(Plot) - traits_view = View( - Item("plot", editor=ComponentEditor()), width=600, height=600 - ) - - def __init__(self, index, value, *args, **kw): - super(PlotExample, self).__init__(*args, **kw) + def _plot_default(self): + index = numpy.arange(0, 25, 0.25) + value = numpy.sin(index) + numpy.arange(0, 10, 0.1) plot_data = ArrayPlotData(index=index) plot_data.set_data("value", value) - self.plot = Plot(plot_data) - line = self.plot.plot(("index", "value"))[0] + plot = Plot(plot_data) + line = plot.plot(("index", "value"))[0] line.overlays.append(XRayOverlay(line)) line.tools.append(BoxSelectTool(line)) + return plot + + traits_view = View( + Item("plot", editor=ComponentEditor()), width=600, height=600 + ) -index = numpy.arange(0, 25, 0.25) -value = numpy.sin(index) + numpy.arange(0, 10, 0.1) -example = PlotExample(index, value) +example = PlotExample() example.configure_traits()