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

« back to all changes in this revision

Viewing changes to examples/demo/advanced/variable_sized_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
 
Draws a scatterplot of a set of random points of variable size.
3
 
 - This uses the non-standard renderer, VariableSizeScatterPlot
4
 
 - Left-drag pans the plot.
5
 
 - Mousewheel up and down zooms the plot in and out.
6
 
 - Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular
7
 
   region to zoom.  If you use a sequence of zoom boxes, pressing alt-left-arrow
8
 
   and alt-right-arrow moves you forwards and backwards through the "zoom
9
 
   history".
10
 
"""
11
 
 
12
 
# Major library imports
13
 
import numpy
14
 
import numpy.random
15
 
 
16
 
# Enthought library imports
17
 
from enable.api import Component, ComponentEditor
18
 
from traits.api import HasTraits, Instance
19
 
from traitsui.api import Item, Group, View
20
 
 
21
 
# Chaco imports
22
 
from chaco.api import ArrayPlotData, Plot, VariableSizeScatterPlot, \
23
 
        LinearMapper, ArrayDataSource
24
 
from chaco.tools.api import PanTool, ZoomTool
25
 
 
26
 
#===============================================================================
27
 
# # Create the Chaco plot.
28
 
#===============================================================================
29
 
def _create_plot_component():
30
 
 
31
 
    # Create some data
32
 
    numpts = 1000
33
 
    x = numpy.arange(0, numpts)
34
 
    y = numpy.random.random(numpts)
35
 
    marker_size = numpy.random.normal(4.0, 4.0, numpts)
36
 
 
37
 
    # Create a plot data object and give it this data
38
 
    pd = ArrayPlotData()
39
 
    pd.set_data("index", x)
40
 
    pd.set_data("value", y)
41
 
 
42
 
    # Because this is a non-standard renderer, we can't call plot.plot, which
43
 
    # sets up the array data sources, mappers and default index/value ranges.
44
 
    # So, its gotta be done manually for now.
45
 
 
46
 
    index_ds = ArrayDataSource(x)
47
 
    value_ds = ArrayDataSource(y)
48
 
 
49
 
    # Create the plot
50
 
    plot = Plot(pd)
51
 
    plot.index_range.add(index_ds)
52
 
    plot.value_range.add(value_ds)
53
 
 
54
 
    # Create the index and value mappers using the plot data ranges
55
 
    imapper = LinearMapper(range=plot.index_range)
56
 
    vmapper = LinearMapper(range=plot.value_range)
57
 
 
58
 
    # Create the scatter renderer
59
 
    scatter = VariableSizeScatterPlot(
60
 
                    index=index_ds,
61
 
                    value=value_ds,
62
 
                    index_mapper = imapper,
63
 
                    value_mapper = vmapper,
64
 
                    marker='circle',
65
 
                    marker_size=marker_size,
66
 
                    color=(1.0,0.0,0.75,0.4))
67
 
 
68
 
    # Append the renderer to the list of the plot's plots
69
 
    plot.add(scatter)
70
 
    plot.plots['var_size_scatter'] = [scatter]
71
 
 
72
 
    # Tweak some of the plot properties
73
 
    plot.title = "Scatter Plot"
74
 
    plot.line_width = 0.5
75
 
    plot.padding = 50
76
 
 
77
 
    # Attach some tools to the plot
78
 
    plot.tools.append(PanTool(plot, constrain_key="shift"))
79
 
    zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
80
 
    plot.overlays.append(zoom)
81
 
 
82
 
    return plot
83
 
 
84
 
#===============================================================================
85
 
# Attributes to use for the plot view.
86
 
size = (650, 650)
87
 
title = "Basic scatter plot"
88
 
bg_color="lightgray"
89
 
 
90
 
#===============================================================================
91
 
# # Demo class that is used by the demo.py application.
92
 
#===============================================================================
93
 
class Demo(HasTraits):
94
 
    plot = Instance(Component)
95
 
 
96
 
    traits_view = View(
97
 
                    Group(
98
 
                        Item('plot', editor=ComponentEditor(size=size,
99
 
                                                            bgcolor=bg_color),
100
 
                             show_label=False),
101
 
                        orientation = "vertical"),
102
 
                    resizable=True, title=title
103
 
                    )
104
 
 
105
 
    def _plot_default(self):
106
 
         return _create_plot_component()
107
 
 
108
 
demo = Demo()
109
 
 
110
 
if __name__ == "__main__":
111
 
    demo.configure_traits()
112
 
 
113
 
#--EOF---