Skip to content

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

@rahulporuri

Description

@rahulporuri

The chaco examples create the displayed plot object in atleast two different ways - they either override the __init__ method of the underlying traits class to create and store the plot object or they define a plot trait and a _plot_default method which creates the plot object.

I think the latter way is preferred over the former - we don't want to unnecessarily override the __init__ method.

def __init__(self):
# Create the data and the PlotData object
x = linspace(-14, 14, 100)
y = sin(x) * x ** 3
plotdata = ArrayPlotData(x=x, y=y)
# Create a scatter plot
scatter_plot = Plot(plotdata)
scatter_plot.plot(("x", "y"), type="scatter", color="blue")
# Create a line plot
line_plot = Plot(plotdata)
line_plot.plot(("x", "y"), type="line", color="blue")
# Create a horizontal container and put the two plots inside it
container = HPlotContainer(line_plot, scatter_plot, spacing=100)
self.plot = container

def _plot_default(self):
y = self.x ** self.power
plot_data = ArrayPlotData(x=self.x, y=y)
plot = Plot(plot_data)
plot.plot(("x", "y"), "line", name="power function", color="auto")
# configure the plot
plot.padding_top = 25
plot.border_visible = False
plot.index_grid.visible = False
plot.value_grid.visible = False
plot.title = "Power Function n={}".format(self.power)
plot.title_position = "right"
plot.title_angle = -90
plot.legend_alignment = "ul"
plot.legend.border_visible = False
plot.legend.bgcolor = (0.9, 0.9, 0.9, 0.5)
plot.legend.visible = True
plot.index_axis.title = "y"
plot.value_axis.title = "x"
return plot

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions