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

« back to all changes in this revision

Viewing changes to examples/demo/basic/zoomable_colorbar.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 zoomable/pannable colormap
3
 
 
4
 
Draws a colormapped scatterplot of random data. The colormap is dynamically
5
 
adjustable.
6
 
 
7
 
In addition to normal zooming and panning on the plot, the user can also
8
 
pan and zoom the colorbar to change the color mapping of the data values.
9
 
 
10
 
Left click will pan the colorbar's data region.  Right-drag will
11
 
select a zoom range.  Mousewheel up and down will zoom in and out on
12
 
the data bounds of the color bar.
13
 
"""
14
 
 
15
 
# Major library imports
16
 
from numpy import exp, sort
17
 
from numpy.random import random
18
 
 
19
 
# Enthought library imports
20
 
from enable.api import Component, ComponentEditor
21
 
from traits.api import HasTraits, Instance
22
 
from traitsui.api import Item, Group, View
23
 
 
24
 
# Chaco imports
25
 
from chaco.api import ArrayPlotData, ColorBar, \
26
 
                                 HPlotContainer, \
27
 
                                 LinearMapper, Plot, gist_earth
28
 
from chaco.tools.api import PanTool, ZoomTool
29
 
 
30
 
#===============================================================================
31
 
# # Create the Chaco plot.
32
 
#===============================================================================
33
 
def _create_plot_component():
34
 
 
35
 
    # Create some data
36
 
    numpts = 1000
37
 
    x = sort(random(numpts))
38
 
    y = random(numpts)
39
 
    color = exp(-(x**2 + y**2))
40
 
 
41
 
    # Create a plot data obect and give it this data
42
 
    pd = ArrayPlotData()
43
 
    pd.set_data("index", x)
44
 
    pd.set_data("value", y)
45
 
    pd.set_data("color", color)
46
 
 
47
 
    # Create the plot
48
 
    plot = Plot(pd)
49
 
    plot.plot(("index", "value", "color"),
50
 
              type="cmap_scatter",
51
 
              name="my_plot",
52
 
              color_mapper=gist_earth,
53
 
              marker = "square",
54
 
              fill_alpha = 0.5,
55
 
              marker_size = 8,
56
 
              outline_color = "black",
57
 
              border_visible = True,
58
 
              bgcolor = "white")
59
 
 
60
 
    # Tweak some of the plot properties
61
 
    plot.title = "Colormapped Scatter Plot with Pan/Zoom Color Bar"
62
 
    plot.padding = 50
63
 
    plot.x_grid.visible = False
64
 
    plot.y_grid.visible = False
65
 
    plot.x_axis.font = "modern 16"
66
 
    plot.y_axis.font = "modern 16"
67
 
 
68
 
    # Add pan and zoom to the plot
69
 
    plot.tools.append(PanTool(plot, constrain_key="shift"))
70
 
    zoom = ZoomTool(plot)
71
 
    plot.overlays.append(zoom)
72
 
 
73
 
    # Create the colorbar, handing in the appropriate range and colormap
74
 
    colorbar = ColorBar(index_mapper=LinearMapper(range=plot.color_mapper.range),
75
 
                        color_mapper=plot.color_mapper,
76
 
                        orientation='v',
77
 
                        resizable='v',
78
 
                        width=30,
79
 
                        padding=20)
80
 
    colorbar.plot = plot
81
 
    colorbar.padding_top = plot.padding_top
82
 
    colorbar.padding_bottom = plot.padding_bottom
83
 
 
84
 
    # Add pan and zoom tools to the colorbar
85
 
    colorbar.tools.append(PanTool(colorbar, constrain_direction="y", constrain=True))
86
 
    zoom_overlay = ZoomTool(colorbar, axis="index", tool_mode="range",
87
 
                            always_on=True, drag_button="right")
88
 
    colorbar.overlays.append(zoom_overlay)
89
 
 
90
 
    # Create a container to position the plot and the colorbar side-by-side
91
 
    container = HPlotContainer(plot, colorbar, use_backbuffer=True, bgcolor="lightgray")
92
 
 
93
 
    return container
94
 
 
95
 
#===============================================================================
96
 
# Attributes to use for the plot view.
97
 
size=(650,650)
98
 
title="Colormapped scatter plot"
99
 
 
100
 
#===============================================================================
101
 
# # Demo class that is used by the demo.py application.
102
 
#===============================================================================
103
 
class Demo(HasTraits):
104
 
    plot = Instance(Component)
105
 
 
106
 
    traits_view = View(
107
 
                    Group(
108
 
                        Item('plot', editor=ComponentEditor(size=size),
109
 
                             show_label=False),
110
 
                        orientation = "vertical"),
111
 
                    resizable=True, title=title
112
 
                    )
113
 
 
114
 
    def _plot_default(self):
115
 
         return _create_plot_component()
116
 
 
117
 
demo = Demo()
118
 
 
119
 
if __name__ == "__main__":
120
 
    demo.configure_traits()