~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to examples/demo/basic/cmap_scatter.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Scatterplot with range-selectable data points
3
 
 
4
 
Draws a colormapped scatterplot of random data.
5
 
 
6
 
In addition to normal zooming and panning on the plot, the user can select
7
 
a range of data values by right-dragging in the color bar.
8
 
 
9
 
Left-click in the color bar to cancel the range selection.
10
 
"""
11
 
 
12
 
# Major library imports
13
 
from numpy import exp, sort
14
 
from numpy.random import random
15
 
 
16
 
# Enthought library imports
17
 
from enable.api import Component, ComponentEditor, Window
18
 
from traits.api import HasTraits, Instance
19
 
from traitsui.api import Item, VGroup, View, Label
20
 
 
21
 
# Chaco imports
22
 
from chaco.api import ArrayPlotData, ColorBar, \
23
 
                                 ColormappedSelectionOverlay, HPlotContainer, \
24
 
                                 jet, LinearMapper, Plot
25
 
from chaco.tools.api import PanTool, ZoomTool, RangeSelection, \
26
 
                                       RangeSelectionOverlay
27
 
 
28
 
#===============================================================================
29
 
# # Create the Chaco plot.
30
 
#===============================================================================
31
 
def _create_plot_component():
32
 
 
33
 
    # Create some data
34
 
    numpts = 1000
35
 
    x = sort(random(numpts))
36
 
    y = random(numpts)
37
 
    color = exp(-(x**2 + y**2))
38
 
 
39
 
    # Create a plot data obect and give it this data
40
 
    pd = ArrayPlotData()
41
 
    pd.set_data("index", x)
42
 
    pd.set_data("value", y)
43
 
    pd.set_data("color", color)
44
 
 
45
 
    # Create the plot
46
 
    plot = Plot(pd)
47
 
    plot.plot(("index", "value", "color"),
48
 
              type="cmap_scatter",
49
 
              name="my_plot",
50
 
              color_mapper=jet,
51
 
              marker = "square",
52
 
              fill_alpha = 0.5,
53
 
              marker_size = 6,
54
 
              outline_color = "black",
55
 
              border_visible = True,
56
 
              bgcolor = "white")
57
 
 
58
 
    # Tweak some of the plot properties
59
 
    plot.title = "Colormapped Scatter Plot with Range-selectable Data Points"
60
 
    plot.padding = 50
61
 
    plot.x_grid.visible = False
62
 
    plot.y_grid.visible = False
63
 
    plot.x_axis.font = "modern 16"
64
 
    plot.y_axis.font = "modern 16"
65
 
 
66
 
    # Right now, some of the tools are a little invasive, and we need the
67
 
    # actual ColomappedScatterPlot object to give to them
68
 
    cmap_renderer = plot.plots["my_plot"][0]
69
 
 
70
 
    # Attach some tools to the plot
71
 
    plot.tools.append(PanTool(plot, constrain_key="shift"))
72
 
    zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
73
 
    plot.overlays.append(zoom)
74
 
    selection = ColormappedSelectionOverlay(cmap_renderer, fade_alpha=0.35,
75
 
                                            selection_type="mask")
76
 
    cmap_renderer.overlays.append(selection)
77
 
 
78
 
    # Create the colorbar, handing in the appropriate range and colormap
79
 
    colorbar = create_colorbar(plot.color_mapper)
80
 
    colorbar.plot = cmap_renderer
81
 
    colorbar.padding_top = plot.padding_top
82
 
    colorbar.padding_bottom = plot.padding_bottom
83
 
 
84
 
    # Create a container to position the plot and the colorbar side-by-side
85
 
    container = HPlotContainer(use_backbuffer = True)
86
 
    container.add(plot)
87
 
    container.add(colorbar)
88
 
    container.bgcolor = "lightgray"
89
 
    return container
90
 
 
91
 
def create_colorbar(colormap):
92
 
    colorbar = ColorBar(index_mapper=LinearMapper(range=colormap.range),
93
 
                        color_mapper=colormap,
94
 
                        orientation='v',
95
 
                        resizable='v',
96
 
                        width=30,
97
 
                        padding=20)
98
 
    colorbar.tools.append(RangeSelection(component=colorbar))
99
 
    colorbar.overlays.append(RangeSelectionOverlay(component=colorbar,
100
 
                                                   border_color="white",
101
 
                                                   alpha=0.8,
102
 
                                                   fill_color="lightgray"))
103
 
    return colorbar
104
 
 
105
 
#===============================================================================
106
 
# Attributes to use for the plot view.
107
 
size=(650,650)
108
 
title="Colormapped scatter plot"
109
 
 
110
 
#===============================================================================
111
 
# # Demo class that is used by the demo.py application.
112
 
#===============================================================================
113
 
class Demo(HasTraits):
114
 
    plot = Instance(Component)
115
 
 
116
 
    traits_view = View(
117
 
                    VGroup(
118
 
                        Label('Right-drag on colorbar to select data range'),
119
 
                        Item('plot', editor=ComponentEditor(size=size),
120
 
                             show_label=False),
121
 
                        ),
122
 
                    resizable=True, 
123
 
                    title=title
124
 
                    )
125
 
    def _plot_default(self):
126
 
         return _create_plot_component()
127
 
 
128
 
demo = Demo()
129
 
 
130
 
if __name__ == "__main__":
131
 
    demo.configure_traits()