"""
Scatter plot with panning and zooming

Shows a scatter plot of a set of random points, 
with basic Chaco panning and zooming.

Interacting with the plot:

  - Left-mouse-drag pans the plot.
  - Mouse wheel up and down zooms the plot in and out.
  - Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular
    region to zoom. If you use a sequence of zoom boxes, pressing alt-left-arrow
    and alt-right-arrow moves you forwards and backwards through the "zoom
    history".
"""

# Major library imports
from numpy import sort
from numpy.random import random

# Enthought library imports
from enable.api import Component, ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import Item, Group, View

# Chaco imports
from chaco.api import ArrayPlotData, Plot
from chaco.tools.api import PanTool, ZoomTool

#===============================================================================
# # Create the Chaco plot.
#===============================================================================
def _create_plot_component():

    # Create some data
    numpts = 5000
    x = sort(random(numpts))
    y = random(numpts)

    # Create a plot data obect and give it this data
    pd = ArrayPlotData()
    pd.set_data("index", x)
    pd.set_data("value", y)

    # Create the plot
    plot = Plot(pd)
    plot.plot(("index", "value"),
              type="scatter",
              marker="circle",
              index_sort="ascending",
              color="orange",
              marker_size=3,
              bgcolor="white")

    # Tweak some of the plot properties
    plot.title = "Scatter Plot"
    plot.line_width = 0.5
    plot.padding = 50

    # Attach some tools to the plot
    plot.tools.append(PanTool(plot, constrain_key="shift"))
    zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
    plot.overlays.append(zoom)

    return plot

#===============================================================================
# Attributes to use for the plot view.
size = (650, 650)
title = "Basic scatter plot"
bg_color="lightgray"

#===============================================================================
# # Demo class that is used by the demo.py application.
#===============================================================================
class Demo(HasTraits):
    plot = Instance(Component)

    traits_view = View(
                    Group(
                        Item('plot', editor=ComponentEditor(size=size,
                                                            bgcolor=bg_color),
                             show_label=False),
                        orientation = "vertical"),
                    resizable=True, title=title
                    )

    def _plot_default(self):
         return _create_plot_component()

demo = Demo()

if __name__ == "__main__":
    demo.configure_traits()

#--EOF---

[((1150, 1164), 'numpy.random.random', 'random', (['numpts'], {}), '(numpts)\n', (1156, 1164), False, 'from numpy.random import random\n'), ((1228, 1243), 'chaco.api.ArrayPlotData', 'ArrayPlotData', ([], {}), '()\n', (1241, 1243), False, 'from chaco.api import ArrayPlotData, Plot\n'), ((1334, 1342), 'chaco.api.Plot', 'Plot', (['pd'], {}), '(pd)\n', (1338, 1342), False, 'from chaco.api import ArrayPlotData, Plot\n'), ((1795, 1853), 'chaco.tools.api.ZoomTool', 'ZoomTool', ([], {'component': 'plot', 'tool_mode': '"""box"""', 'always_on': '(False)'}), "(component=plot, tool_mode='box', always_on=False)\n", (1803, 1853), False, 'from chaco.tools.api import PanTool, ZoomTool\n'), ((2344, 2363), 'traits.api.Instance', 'Instance', (['Component'], {}), '(Component)\n', (2352, 2363), False, 'from traits.api import HasTraits, Instance\n'), ((1126, 1140), 'numpy.random.random', 'random', (['numpts'], {}), '(numpts)\n', (1132, 1140), False, 'from numpy.random import random\n'), ((1746, 1782), 'chaco.tools.api.PanTool', 'PanTool', (['plot'], {'constrain_key': '"""shift"""'}), "(plot, constrain_key='shift')\n", (1753, 1782), False, 'from chaco.tools.api import PanTool, ZoomTool\n'), ((2460, 2504), 'enable.api.ComponentEditor', 'ComponentEditor', ([], {'size': 'size', 'bgcolor': 'bg_color'}), '(size=size, bgcolor=bg_color)\n', (2475, 2504), False, 'from enable.api import Component, ComponentEditor\n')]