chaco step-by-step

53
Chaco step-by-step July 17, 2009 Monday, February 22, 2010

Upload: enthought-inc

Post on 27-May-2015

6.933 views

Category:

Documents


5 download

DESCRIPTION

An introduction into Chaco, the open source interactive 2d plotting library for Python.

TRANSCRIPT

Page 1: Chaco Step-by-Step

Chaco step-by-step

July 17, 2009

Monday, February 22, 2010

Page 2: Chaco Step-by-Step

http://www.enthought.com/

Monday, February 22, 2010

Page 3: Chaco Step-by-Step

Enthought Python Distribution (EPD)

MORE THAN FIFTY INTEGRATED PACKAGES

• Python 2.5.2

• Science (NumPy, SciPy, etc.)

• Plotting (Chaco, Matplotlib)

• Visualization (VTK, Mayavi)

• Multi-language Integration (SWIG,Pyrex, f2py, weave)

• Database (MySQL, SQLLite, etc.)

• Data Storage (HDF, NetCDF, etc.)

• Networking (twisted)

• User Interface (wxPython, Traits UI)

• Enthought Tool Suite (Application Development Tools)

Monday, February 22, 2010

Page 4: Chaco Step-by-Step

Enthought Python Distribution (EPD)

Monday, February 22, 2010

Page 5: Chaco Step-by-Step

Enthought Python Distribution (EPD)

Selections from our training courses including: explanations, demonstrations, and tips

For subscribers to Enthought Python Distribution (EPD)

Offered once a month for 60-90 minutes depending on questions

Monday, February 22, 2010

Page 6: Chaco Step-by-Step

Enthought Training Courses

Python Basics, NumPy, SciPy, Matplotlib, Traits, TraitsUI, Chaco…

Monday, February 22, 2010

Page 7: Chaco Step-by-Step

Upcoming Training ClassesSeptember 21 – 25, 2009 Introduction to Scientific Computing with Python Austin, TexasOctober 19 – 22, 2009 Python for Science, Eng., and Financial Analysis Silicon Valley, CaliforniaNovember 9 – 12, 2009 Python for Science, Eng., and Financial Analysis Chicago, IllinoisDecember 7 – 11, 2009 Introduction to Scientific Computing with Python Austin, Texas

http://www.enthought.com/training/

Monday, February 22, 2010

Page 8: Chaco Step-by-Step

Enthought Tool Suite http://code.enthought.com/

Monday, February 22, 2010

Page 9: Chaco Step-by-Step

DatabaseAccess

ComplianceTools

EquipmentInterface

ScientificAlgorithms

UIElements

Testing Framework Scripting InterfaceChaco

Plotting

Data Display

Rich Client App (Geophysics, Finance, Etc)

Monday, February 22, 2010

Page 10: Chaco Step-by-Step

Monday, February 22, 2010

Page 11: Chaco Step-by-Step

Chaco: Interactive Graphics

Monday, February 22, 2010

Page 12: Chaco Step-by-Step

Introduction

• Chaco is a plotting application toolkit• You can build simple, static plots

Monday, February 22, 2010

Page 13: Chaco Step-by-Step

Introduction

• Chaco is a plotting application toolkit• You can build simple, static plots• You can also build rich, interactive

visualizations:

Monday, February 22, 2010

Page 14: Chaco Step-by-Step

“Script-oriented” Plotting

• from numpy import *• from enthought.chaco.shell import *

• x = linspace(-2*pi, 2*pi, 100)• y = sin(x)

• plot(x, y, 'r-')• title('First plot')• ytitle('sin(x)')• show()

Monday, February 22, 2010

Page 15: Chaco Step-by-Step

“Application-oriented” Plotting

• class LinePlot(HasTraits):• plot = Instance(Plot)• traits_view = View(• Item('plot',editor=ComponentEditor(),• show_label=False), • width=500, height=500,• resizable=True,• title="Chaco Plot")

• def __init__(self):• x = linspace(-14, 14, 100)• y = sin(x) * x**3• plotdata = ArrayPlotData(x = x, y = y)• plot = Plot(plotdata)• plot.plot(("x", "y"), type="line",• color="blue")• plot.title = "sin(x) * x^3"• self.plot = plot

• if __name__ == "__main__":• LinePlot().configure_traits()

Monday, February 22, 2010

Page 16: Chaco Step-by-Step

First Plot

class LinePlot(HasTraits): plot = Instance(Plot) traits_view = View( Item('plot', editor=ComponentEditor(), show_label=False), width=500, height=500, resizable=True, title="Chaco Plot")

def __init__(self): x = linspace(-14, 14, 100) y = sin(x) * x**3 plotdata = ArrayPlotData(x = x, y = y) plot = Plot(plotdata) plot.plot(("x", "y"), type="line", color="blue") plot.title = "sin(x) * x^3" self.plot = plot

Monday, February 22, 2010

Page 17: Chaco Step-by-Step

First Plot

class LinePlot(HasTraits): plot = Instance(Plot) traits_view = View( Item('plot',editor=ComponentEditor(), show_label=False), width=500, height=500, resizable=True, title="Chaco Plot")

def __init__(self): x = linspace(-14, 14, 100) y = sin(x) * x**3 plotdata = ArrayPlotData(x = x, y = y) plot = Plot(plotdata) plot.plot(("x", "y"), type="line", color="blue") plot.title = "sin(x) * x^3" self.plot = plot

Monday, February 22, 2010

Page 18: Chaco Step-by-Step

First Plot

class LinePlot(HasTraits): plot = Instance(Plot) traits_view = View( Item('plot',editor=ComponentEditor(), show_label=False), width=500, height=500, resizable=True, title="Chaco Plot")

def __init__(self): x = linspace(-14, 14, 100) y = sin(x) * x**3 plotdata = ArrayPlotData(x = x, y = y) plot = Plot(plotdata) plot.plot(("x", "y"), type="line", color="blue") plot.title = "sin(x) * x^3" self.plot = plot

Monday, February 22, 2010

Page 19: Chaco Step-by-Step

First Plot

class LinePlot(HasTraits): plot = Instance(Plot) traits_view = View( Item('plot',editor=ComponentEditor(), show_label=False), width=500, height=500, resizable=True, title="Chaco Plot")

def __init__(self): x = linspace(-14, 14, 100) y = sin(x) * x**3 plotdata = ArrayPlotData(x = x, y = y) plot = Plot(plotdata) plot.plot(("x", "y"), type="line", color="blue") plot.title = "sin(x) * x^3" self.plot = plot

Monday, February 22, 2010

Page 20: Chaco Step-by-Step

First Plot

class LinePlot(HasTraits): plot = Instance(Plot) traits_view = View( Item('plot',editor=ComponentEditor(), show_label=False), width=500, height=500, resizable=True, title="Chaco Plot")

def __init__(self): x = linspace(-14, 14, 100) y = sin(x) * x**3 plotdata = ArrayPlotData(x = x, y = y) plot = Plot(plotdata) plot.plot(("x", "y"), type="line", color="blue") plot.title = "sin(x) * x^3" self.plot = plot

Monday, February 22, 2010

Page 21: Chaco Step-by-Step

First Plot

class LinePlot(HasTraits): plot = Instance(Plot) traits_view = View( Item('plot',editor=ComponentEditor(), show_label=False), width=500, height=500, resizable=True, title="Chaco Plot")

def __init__(self): x = linspace(-14, 14, 100) y = sin(x) * x**3 plotdata = ArrayPlotData(x = x, y = y) plot = Plot(plotdata) plot.plot(("x", "y"), type="line", color="blue") plot.title = "sin(x) * x^3" self.plot = plot

Monday, February 22, 2010

Page 22: Chaco Step-by-Step

First Plot

class LinePlot(HasTraits): plot = Instance(Plot) traits_view = View( Item('plot',editor=ComponentEditor(), show_label=False), width=500, height=500, resizable=True, title="Chaco Plot")

def __init__(self): x = linspace(-14, 14, 100) y = sin(x) * x**3 plotdata = ArrayPlotData(x = x, y = y) plot = Plot(plotdata) plot.plot(("x", "y"), type="line", color="blue") plot.title = "sin(x) * x^3" self.plot = plot

Monday, February 22, 2010

Page 23: Chaco Step-by-Step

First Plot

Finally, do something when this script is executed:

if __name__ == "__main__":

LinePlot().configure_traits()

Monday, February 22, 2010

Page 24: Chaco Step-by-Step

Scatter Plot

• class ScatterPlot(HasTraits):• plot = Instance(Plot)• traits_view = View(• Item('plot', editor=ComponentEditor(),• show_label=False), • width=500, height=500,• resizable=True,• title="Chaco Plot")

• def __init__(self):• x = linspace(-14, 14, 100)• y = sin(x) * x**3• plotdata = ArrayPlotData(x = x, y = y)• plot = Plot(plotdata)• plot.plot(("x", "y"), type="scatter",• color="blue")• self.plot = plot

• if __name__ == "__main__":

Monday, February 22, 2010

Page 25: Chaco Step-by-Step

Image Plot

• class ImagePlot(HasTraits):• plot = Instance(Plot)• traits_view = View(• Item('plot', editor=ComponentEditor(),• show_label=False), • width=500, height=500,• resizable=True,• title=”Chaco Plot”)

• def __init__(self):• x = linspace(0, 10, 50)• y = linspace(0, 5, 50)• xgrid, ygrid = meshgrid(x, y)• z = exp(-(xgrid*xgrid+ygrid*ygrid)/100)• plotdata = ArrayPlotData(imagedata = z)• plot = Plot(plotdata)• plot.img_plot("imagedata", xbounds=x,• ybounds=y, colormap=jet)• self.plot = plot

• if __name__ == "__main__":• ImagePlot().configure_traits()

Monday, February 22, 2010

Page 26: Chaco Step-by-Step

A Slight Modification

• class OverlappingPlot(HasTraits):• plot = Instance(Plot)• traits_view = View(• Item('plot',editor=ComponentEditor(),• show_label=False), • width=500, height=500,• resizable=True,• title=”Chaco Plot”)

• def __init__(self):• x = linspace(-14, 14, 100)• y = x/2 * sin(x)• y2 = cos(x)• plotdata = ArrayPlotData(x=x,y=y,y2=y2)• plot = Plot(plotdata)• plot.plot(("x", "y"), type="scatter",• color="blue")• plot.plot(("x", "y2"), type="line",• color="red")• self.plot = plot

• if __name__ == "__main__":• OverlappingPlot().configure_traits()

Monday, February 22, 2010

Page 27: Chaco Step-by-Step

Editing Plot Traits• from enthought.traits.api import Int• from enthought.enable.api import ColorTrait• from enthought.chaco.api import marker_trait

• class ScatterPlotTraits(HasTraits):• plot = Instance(Plot)• color = ColorTrait("blue")• marker = marker_trait• marker_size = Int(4)

Monday, February 22, 2010

Page 28: Chaco Step-by-Step

Editing Plot Traits• from enthought.traits.api import Int• from enthought.enable.api import ColorTrait• from enthought.chaco.api import marker_trait

• class ScatterPlotTraits(HasTraits):• plot = Instance(Plot)• color = ColorTrait("blue")• marker = marker_trait• marker_size = Int(4)• traits_view = View(• Group(• Item('color', label="Color", style="custom"),• Item('marker', label="Marker"),• Item('marker_size', label="Size"),• Item('plot', editor=ComponentEditor(), • show_label=False),• orientation = "vertical"• ),• width=500, height=500,• resizable=True,• title="Chaco Plot")

Monday, February 22, 2010

Page 29: Chaco Step-by-Step

Editing Plot Traits• def __init__(self):• • x = linspace(-14, 14, 100)• y = sin(x) * x**3• plotdata = ArrayPlotData(x = x, y = y)• • plot = Plot(plotdata)• • self.renderer = plot.plot(("x", "y"), type="scatter", • color="blue")[0]• self.plot = plot

Monday, February 22, 2010

Page 30: Chaco Step-by-Step

Editing Plot Traits• def __init__(self):• • x = linspace(-14, 14, 100)• y = sin(x) * x**3• plotdata = ArrayPlotData(x = x, y = y)• • plot = Plot(plotdata)• • self.renderer = plot.plot(("x", "y"), type="scatter", • color="blue")[0]• self.plot = plot

• def _color_changed(self):• self.renderer.color = self.color

Monday, February 22, 2010

Page 31: Chaco Step-by-Step

Editing Plot Traits• def __init__(self):• • x = linspace(-14, 14, 100)• y = sin(x) * x**3• plotdata = ArrayPlotData(x = x, y = y)• • plot = Plot(plotdata)• • self.renderer = plot.plot(("x", "y"), type="scatter", • color="blue")[0]• self.plot = plot

• def _color_changed(self):• self.renderer.color = self.color

• def _marker_changed(self):• self.renderer.marker = self.marker

• def _marker_size_changed(self):• self.renderer.marker_size = self.marker_size

Monday, February 22, 2010

Page 32: Chaco Step-by-Step

Editing Plot Traits

Monday, February 22, 2010

Page 33: Chaco Step-by-Step

Toolsfrom enthought.chaco.tools.api import PanTool, ZoomTool, DragZoom

class ToolsExample(HasTraits): plot = Instance(Plot) traits_view = View( Item('plot',editor=ComponentEditor(), show_label=False), width=500, height=500, resizable=True, title="Chaco Plot")

def __init__(self): x = linspace(-14, 14, 100) y = sin(x) * x**3 plotdata = ArrayPlotData(x = x, y = y) plot = Plot(plotdata) plot.plot(("x", "y"), type="line", color="blue")

plot.tools.append(PanTool(plot)) plot.tools.append(ZoomTool(plot)) plot.tools.append(DragZoom(plot, drag_button="right"))

self.plot = plot

Monday, February 22, 2010

Page 34: Chaco Step-by-Step

Tool Chooserfrom enthought.traits.ui.api import CheckListEditor

class ToolsExample(HasTraits): plot = Instance(Plot) tools = List(editor=CheckListEditor(values = ["PanTool", "SimpleZoom", "DragZoom"]))

Monday, February 22, 2010

Page 35: Chaco Step-by-Step

Tool Chooserfrom enthought.traits.ui.api import CheckListEditor

class ToolsExample(HasTraits): plot = Instance(Plot) tools = List(editor=CheckListEditor(values = ["PanTool", "SimpleZoom", "DragZoom"]))

def __init__(self): x = linspace(-14, 14, 500) y = sin(x) * x**3 plotdata = ArrayPlotData(x = x, y = y) plot = Plot(plotdata) plot.plot(("x", "y"), type="line", color="blue")

plot.tools.append(PanTool(plot)) plot.tools.append(ZoomTool(plot)) plot.tools.append(DragZoom(plot, drag_button="right"))

self.plot = plot

Monday, February 22, 2010

Page 36: Chaco Step-by-Step

Tool Chooser

def _tools_changed(self): classes = [eval(class_name) for class_name in self.tools]

for tool in self.plot.tools: if tool.__class__ not in classes: self.plot.tools.remove(tool) else: classes.remove(tool.__class__)

for cls in classes: self.plot.tools.append(cls(self.plot)) return

Monday, February 22, 2010

Page 37: Chaco Step-by-Step

Tool Chooser

def _tools_changed(self): classes = [eval(class_name) for class_name in self.tools]

for tool in self.plot.tools: if tool.__class__ not in classes: self.plot.tools.remove(tool) else: classes.remove(tool.__class__)

for cls in classes: self.plot.tools.append(cls(self.plot)) return

Monday, February 22, 2010

Page 38: Chaco Step-by-Step

Tool Chooser

def _tools_changed(self): classes = [eval(class_name) for class_name in self.tools]

for tool in self.plot.tools: if tool.__class__ not in classes: self.plot.tools.remove(tool) else: classes.remove(tool.__class__)

for cls in classes: self.plot.tools.append(cls(self.plot)) return

Monday, February 22, 2010

Page 39: Chaco Step-by-Step

Tool Chooser

def _tools_changed(self): classes = [eval(class_name) for class_name in self.tools]

for tool in self.plot.tools: if tool.__class__ not in classes: self.plot.tools.remove(tool) else: classes.remove(tool.__class__)

for cls in classes: self.plot.tools.append(cls(self.plot)) return

Monday, February 22, 2010

Page 40: Chaco Step-by-Step

Tool Chooser

def _tools_changed(self): classes = [eval(class_name) for class_name in self.tools]

for tool in self.plot.tools: if tool.__class__ not in classes: self.plot.tools.remove(tool) else: classes.remove(tool.__class__)

for cls in classes: self.plot.tools.append(cls(self.plot)) return

Monday, February 22, 2010

Page 41: Chaco Step-by-Step

Tool Chooser

def _tools_changed(self): classes = [eval(class_name) for class_name in self.tools]

for tool in self.plot.tools: if tool.__class__ not in classes: self.plot.tools.remove(tool) else: classes.remove(tool.__class__)

for cls in classes: self.plot.tools.append(cls(self.plot)) return

Monday, February 22, 2010

Page 42: Chaco Step-by-Step

Tool Chooser

def _tools_changed(self): classes = [eval(class_name) for class_name in self.tools]

for tool in self.plot.tools: if tool.__class__ not in classes: self.plot.tools.remove(tool) else: classes.remove(tool.__class__)

for cls in classes: self.plot.tools.append(cls(self.plot)) return

Monday, February 22, 2010

Page 43: Chaco Step-by-Step

Tool Chooserclass ToolChooserExample(HasTraits):

plot = Instance(Plot) tools = List(editor=CheckListEditor(values = ["PanTool", "ZoomTool", "DragZoom"])) traits_view = View(Item("tools", label="Tools", style="custom"), Item('plot', editor=ComponentEditor(), show_label=False), width=800, height=600, resizable=True, title="Tool Chooser")

def __init__(self): ... def _tools_changed(self): classes = [eval(class_name) for class_name in self.tools] # Remove all tools that are not in the enabled list in self.tools for tool in self.plot.tools: if tool.__class__ not in classes: self.plot.tools.remove(tool) else: classes.remove(tool.__class__) # Create new instances of tools for the remaining tool classes for cls in classes: self.plot.tools.append(cls(self.plot)) return

Monday, February 22, 2010

Page 44: Chaco Step-by-Step

Tool Chooser

Monday, February 22, 2010

Page 45: Chaco Step-by-Step

Writing a Custom Toolfrom enthought.enable.api import BaseTool

class CustomTool(BaseTool): def normal_mouse_move(self, event): print "Screen point:", event.x, event.y

class ScatterPlot(HasTraits): plot = Instance(Plot) traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False), width=800, height=600, resizable=True, title="Custom Tool")

def _plot_default(self): x = linspace(-14, 14, 100) y = sin(x) * x**3 plotdata = ArrayPlotData(x = x, y = y) plot = Plot(plotdata) plot.plot(("x", "y"), type="scatter", color="blue") plot.tools.append(CustomTool(plot)) return plot

Monday, February 22, 2010

Page 46: Chaco Step-by-Step

Writing a Custom Toolfrom enthought.enable.api import BaseTool

class CustomTool(BaseTool): def normal_mouse_move(self, event): print "Screen point:", event.x, event.y

class ScatterPlot(HasTraits): plot = Instance(Plot) traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False), width=800, height=600, resizable=True, title="Custom Tool")

def _plot_default(self): x = linspace(-14, 14, 100) y = sin(x) * x**3 plotdata = ArrayPlotData(x = x, y = y) plot = Plot(plotdata) plot.plot(("x", "y"), type="scatter", color="blue") plot.tools.append(CustomTool(plot)) return plot

Monday, February 22, 2010

Page 47: Chaco Step-by-Step

Writing a Custom Toolfrom enthought.enable.api import BaseTool

class CustomTool(BaseTool): def normal_mouse_move(self, event): print "Screen point:", event.x, event.y

class ScatterPlot(HasTraits): plot = Instance(Plot) traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False), width=800, height=600, resizable=True, title=”Custom Tool”)

def _plot_default(self): x = linspace(-14, 14, 100) y = sin(x) * x**3 plotdata = ArrayPlotData(x = x, y = y) plot = Plot(plotdata) plot.plot(("x", "y"), type="scatter", color="blue") plot.tools.append(CustomTool(plot)) return plot

Monday, February 22, 2010

Page 48: Chaco Step-by-Step

Writing a Custom Tool

class CustomTool(BaseTool): def normal_mouse_move(self, event): print "Screen point:", event.x, event.y

def normal_left_down(self, event): print "Mouse went down at", event.x, event.y

def normal_left_up(self, event): print "Mouse went up at:", event.x, event.y

Monday, February 22, 2010

Page 49: Chaco Step-by-Step

Writing a Custom Tool

class CustomTool(BaseTool): event_state = Enum("normal", "mousedown")

def normal_mouse_move(self, event): print "Screen:", event.x, event.y

def normal_left_down(self, event): self.event_state = "mousedown" event.handled = True def mousedown_left_up(self, event): self.event_state = "normal" event.handled = True

Monday, February 22, 2010

Page 50: Chaco Step-by-Step

Writing a Custom Tool

class CustomTool(BaseTool):

event_state = Enum("normal", "mousedown")

def normal_mouse_move(self, event): print "Screen:", event.x, event.y

def normal_left_down(self, event): self.event_state = "mousedown" event.handled = True

def mousedown_mouse_move(self, event): print "Data:", self.component.map_data((event.x, event.y))

def mousedown_left_up(self, event): self.event_state = "normal" event.handled = True

Monday, February 22, 2010

Page 51: Chaco Step-by-Step

Additional Tutorial ExamplesCustom Overlay

Monday, February 22, 2010

Page 52: Chaco Step-by-Step

Additional Tutorial Examples

Custom Overlay with Dataspace Option

Monday, February 22, 2010

Page 53: Chaco Step-by-Step

More information• Web page:

http://code.enthought.com/projects/chaco

• Wiki: https://svn.enthought.com/enthought/wiki/ChacoProject

• Gallery: http://code.enthought.com/projects/chaco/gallery.php

• Mailing lists: [email protected], [email protected]

Monday, February 22, 2010