2
from numpy import linspace, sin
4
from enthought.chaco.api import ArrayPlotData, Plot, AbstractOverlay
5
from enthought.chaco.tools.api import PanTool
6
from enthought.enable.component_editor import ComponentEditor
7
from enthought.enable.api import ColorTrait
8
from enthought.traits.api import CArray, Bool, Float, Range, HasTraits, Instance
9
from enthought.traits.ui.api import Item, View, Group, RangeEditor
11
class CustomOverlay(AbstractOverlay):
12
x = Float(10, editor=RangeEditor(low=1.0, high=600, mode="slider"))
13
y = Float(10, editor=RangeEditor(low=1.0, high=500, mode="slider"))
14
width = Range(10.0, 300, editor=RangeEditor(low=10.0, high=300, mode="slider"))
15
height = Range(10.0, 300, editor=RangeEditor(low=10.0, high=300, mode="slider"))
16
color = ColorTrait("red")
17
dataspace = Bool(False)
21
traits_view = View(Group(
22
Item("x"), Item("y"), Item("width"), Item("height"),
24
Item("dataspace", label="Data space?"),
25
orientation = "vertical"
28
def overlay(self, component, gc, view_bounds=None, mode="normal"):
30
self.x, self.y = component.map_screen(self._anchor)
31
gc.set_fill_color(self.color_)
32
x = self.x + component.x
33
y = self.y + component.y
34
gc.rect(x, y, self.width, self.height)
37
def _anytrait_changed(self):
38
self.component.request_redraw()
40
def _dataspace_changed(self):
42
# Map our current x,y point into data space
43
self._anchor = self.component.map_data((self.x, self.y))
45
class ScatterPlot(HasTraits):
49
traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False),
50
width=800, height=600, resizable=True)
52
def _plot_default(self):
53
# Create the data and the PlotData object
54
x = linspace(-14, 14, 100)
56
plotdata = ArrayPlotData(x = x, y = y)
57
# Create a Plot and associate it with the PlotData
59
# Create a scatter plot in the Plot
60
plot.plot(("x", "y"), type="scatter", color="blue")
61
plot.tools.append(PanTool(plot))
62
# Add our custom tool to the plot
63
plot.overlays.append(CustomOverlay(plot))
66
if __name__ == "__main__":
67
# Create the main plot and bring it up via calling edit_traits()
69
plot.edit_traits(kind="live")
70
# Get a handle to the overlay and edit the traits on it as well
71
overlay = plot.plot.overlays[-1]
72
overlay.configure_traits()