Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a plot default method instead of overriding the init method in examples #704

Merged
merged 12 commits into from
Apr 21, 2021
3 changes: 1 addition & 2 deletions examples/demo/advanced/data_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions examples/demo/aspect_ratio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down
43 changes: 23 additions & 20 deletions examples/demo/basic/bar_plot_stacked.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -54,25 +49,33 @@ 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)),
labels=["jan", "feb", "march", "april", "may"],
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()
15 changes: 7 additions & 8 deletions examples/demo/depth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
38 changes: 20 additions & 18 deletions examples/demo/domain_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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()
37 changes: 18 additions & 19 deletions examples/demo/multi_line_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
38 changes: 19 additions & 19 deletions examples/demo/status_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
30 changes: 16 additions & 14 deletions examples/demo/toolbar_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()
22 changes: 11 additions & 11 deletions examples/demo/xray_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()