Chaco step-by-step


                                 July 17, 2009



Monday, February 22, 2010
http://www.enthought.com/




Monday, February 22, 2010
Enthought Python Distribution (EPD)

    MORE THAN FIFTY INTEGRATED PACKAGES

      • Python 2.5.2                   • Database (MySQL, SQLLite, etc.)
      • Science (NumPy, SciPy, etc.)   • Data Storage (HDF, NetCDF, etc.)
      • Plotting (Chaco, Matplotlib)   • Networking (twisted)
      • Visualization (VTK, Mayavi)    • User Interface (wxPython, Traits UI)
      • Multi-language Integration     • Enthought Tool Suite
        (SWIG,Pyrex, f2py, weave)        (Application Development Tools)




Monday, February 22, 2010
Enthought Python Distribution (EPD)




Monday, February 22, 2010
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
Enthought Training Courses




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



Monday, February 22, 2010
Upcoming Training Classes
      September 21 – 25, 2009
           Introduction to Scientific Computing with Python
           Austin, Texas
      October 19 – 22, 2009
           Python for Science, Eng., and Financial Analysis
           Silicon Valley, California
      November 9 – 12, 2009
           Python for Science, Eng., and Financial Analysis
           Chicago, Illinois
      December 7 – 11, 2009
           Introduction to Scientific Computing with Python
           Austin, Texas
                             http://www.enthought.com/training/



Monday, February 22, 2010
Enthought Tool Suite        http://code.enthought.com/




Monday, February 22, 2010
Rich Client App (Geophysics, Finance, Etc)
               Testing Framework Scripting Interface                   Data Display
         Equipment              Compliance     Scientific   Database   Chaco         UI
          Interface               Tools       Algorithms     Access    Plotting   Elements




Monday, February 22, 2010
Monday, February 22, 2010
Chaco: Interactive Graphics




Monday, February 22, 2010
Introduction


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




Monday, February 22, 2010
Introduction

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




Monday, February 22, 2010
“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
“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
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
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
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
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
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
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
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
First Plot


   Finally, do something when this script
   is executed:

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




Monday, February 22, 2010
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
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
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
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
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
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
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
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
Editing Plot Traits




Monday, February 22, 2010
Tools
    from 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
Tool Chooser
    from enthought.traits.ui.api import CheckListEditor

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




Monday, February 22, 2010
Tool Chooser
    from 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
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
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
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
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
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
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
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
Tool Chooser
  class 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
Tool Chooser




Monday, February 22, 2010
Writing a Custom Tool
    from 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
Writing a Custom Tool
    from 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
Writing a Custom Tool
    from 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
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
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
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
Additional Tutorial Examples
                            Custom Overlay




Monday, February 22, 2010
Additional Tutorial Examples
               Custom Overlay with Dataspace Option




Monday, February 22, 2010
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:
          enthought-dev@enthought.com,
          chaco-users@enthought.com




Monday, February 22, 2010

Chaco Step-by-Step

  • 1.
    Chaco step-by-step July 17, 2009 Monday, February 22, 2010
  • 2.
  • 3.
    Enthought Python Distribution(EPD) MORE THAN FIFTY INTEGRATED PACKAGES • Python 2.5.2 • Database (MySQL, SQLLite, etc.) • Science (NumPy, SciPy, etc.) • Data Storage (HDF, NetCDF, etc.) • Plotting (Chaco, Matplotlib) • Networking (twisted) • Visualization (VTK, Mayavi) • User Interface (wxPython, Traits UI) • Multi-language Integration • Enthought Tool Suite (SWIG,Pyrex, f2py, weave) (Application Development Tools) Monday, February 22, 2010
  • 4.
    Enthought Python Distribution(EPD) Monday, February 22, 2010
  • 5.
    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
  • 6.
    Enthought Training Courses Python Basics, NumPy, SciPy, Matplotlib, Traits, TraitsUI, Chaco… Monday, February 22, 2010
  • 7.
    Upcoming Training Classes September 21 – 25, 2009 Introduction to Scientific Computing with Python Austin, Texas October 19 – 22, 2009 Python for Science, Eng., and Financial Analysis Silicon Valley, California November 9 – 12, 2009 Python for Science, Eng., and Financial Analysis Chicago, Illinois December 7 – 11, 2009 Introduction to Scientific Computing with Python Austin, Texas http://www.enthought.com/training/ Monday, February 22, 2010
  • 8.
    Enthought Tool Suite http://code.enthought.com/ Monday, February 22, 2010
  • 9.
    Rich Client App(Geophysics, Finance, Etc) Testing Framework Scripting Interface Data Display Equipment Compliance Scientific Database Chaco UI Interface Tools Algorithms Access Plotting Elements Monday, February 22, 2010
  • 10.
  • 11.
  • 12.
    Introduction • Chaco is a plotting application toolkit • You can build simple, static plots Monday, February 22, 2010
  • 13.
    Introduction • Chaco is a plotting application toolkit • You can build simple, static plots • You can also build rich, interactive visualizations: Monday, February 22, 2010
  • 14.
    “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
  • 15.
    “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
  • 16.
    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
  • 17.
    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
  • 18.
    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
  • 19.
    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
  • 20.
    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
  • 21.
    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
  • 22.
    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
  • 23.
    First Plot Finally, do something when this script is executed: if __name__ == "__main__": LinePlot().configure_traits() Monday, February 22, 2010
  • 24.
    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
  • 25.
    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
  • 26.
    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
  • 27.
    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
  • 28.
    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
  • 29.
    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
  • 30.
    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
  • 31.
    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
  • 32.
    Editing Plot Traits Monday,February 22, 2010
  • 33.
    Tools from 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
  • 34.
    Tool Chooser from enthought.traits.ui.api import CheckListEditor class ToolsExample(HasTraits): plot = Instance(Plot) tools = List(editor=CheckListEditor(values = ["PanTool", "SimpleZoom", "DragZoom"])) Monday, February 22, 2010
  • 35.
    Tool Chooser from 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
  • 36.
    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
  • 37.
    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
  • 38.
    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
  • 39.
    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
  • 40.
    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
  • 41.
    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
  • 42.
    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
  • 43.
    Tool Chooser class 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
  • 44.
  • 45.
    Writing a CustomTool from 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
  • 46.
    Writing a CustomTool from 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
  • 47.
    Writing a CustomTool from 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
  • 48.
    Writing a CustomTool 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
  • 49.
    Writing a CustomTool 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
  • 50.
    Writing a CustomTool 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
  • 51.
    Additional Tutorial Examples Custom Overlay Monday, February 22, 2010
  • 52.
    Additional Tutorial Examples Custom Overlay with Dataspace Option Monday, February 22, 2010
  • 53.
    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