1 | #!/usr/bin/env python |
---|
2 | """ |
---|
3 | Demonstration of drawing a 2D image plot using the "hot" colormap |
---|
4 | """ |
---|
5 | |
---|
6 | #-------------------------------------------------------------------------------- |
---|
7 | # Imports: |
---|
8 | #-------------------------------------------------------------------------------- |
---|
9 | |
---|
10 | import wx |
---|
11 | |
---|
12 | from enthought.traits import Any, Instance |
---|
13 | from enthought.enable.wx import Window |
---|
14 | from enthought.pyface import ApplicationWindow, GUI |
---|
15 | from enthought.util.numerix import pi, concatenate, array, zeros, ones, \ |
---|
16 | arange, resize, ravel |
---|
17 | from enthought.util.numerix import Float as NumericFloat |
---|
18 | from math import sqrt, sin |
---|
19 | |
---|
20 | from enthought.chaco.plot_component import PlotComponent |
---|
21 | from enthought.chaco.plot_axis import PlotAxis |
---|
22 | from enthought.chaco.plot_canvas import PlotCanvas |
---|
23 | from enthought.chaco.plot_group import PlotGroup |
---|
24 | from enthought.chaco.image_plot_value import ImageData, CmapImagePlotValue |
---|
25 | from enthought.chaco.colormap import LinearColormap |
---|
26 | from enthought.chaco.colormap_legend import ColormapLegend |
---|
27 | from enthought.chaco.default_colormaps import hot, gray |
---|
28 | from enthought.chaco.demo.demo_base import PlotApplicationWindow |
---|
29 | |
---|
30 | |
---|
31 | class ImagePlotApplicationWindow( PlotApplicationWindow ): |
---|
32 | |
---|
33 | ########################################################################### |
---|
34 | # PlotApplicationWindow interface. |
---|
35 | ########################################################################### |
---|
36 | |
---|
37 | def _create_plot( self ): |
---|
38 | """ Create the plot to be displayed. """ |
---|
39 | |
---|
40 | # Create the image data and the index values |
---|
41 | #value_grid = zeros((100,100), NumericFloat) |
---|
42 | from testlores2d import get2d_2 |
---|
43 | value_grid = get2d_2() |
---|
44 | #self._compute_function(value_grid) |
---|
45 | index_vals = (arange(value_grid.shape[0]), arange(value_grid.shape[1])) |
---|
46 | |
---|
47 | data = ImageData(value_grid, index_vals) |
---|
48 | print value_grid, index_vals |
---|
49 | |
---|
50 | # Create the index axes |
---|
51 | xaxis = PlotAxis(tick_visible=False, grid_visible=False) |
---|
52 | # bound_low = index_vals[0][0], bound_high = index_vals[0][-1]) |
---|
53 | yaxis = PlotAxis(tick_visible=False, grid_visible=False) |
---|
54 | #bound_low = index_vals[1][0], bound_high = index_vals[1][-1]) |
---|
55 | xaxis.visible = False |
---|
56 | yaxis.visible = False |
---|
57 | |
---|
58 | # Create the value axis (i.e. colormap) |
---|
59 | cmap = hot(0,1) |
---|
60 | |
---|
61 | # Create the Image PlotValue |
---|
62 | # image = CmapImagePlotValue(data, cmap, axis_index = xaxis, axis = yaxis, type='image') |
---|
63 | image = CmapImagePlotValue(data, cmap,type='image') |
---|
64 | image.weight = 10 |
---|
65 | |
---|
66 | cmap_legend = ColormapLegend(cmap, margin_width=31, margin_height=31) |
---|
67 | cmap_legend.weight = 0.4 |
---|
68 | |
---|
69 | group = PlotGroup(cmap_legend, image, orientation='horizontal') |
---|
70 | |
---|
71 | return group |
---|
72 | |
---|
73 | ########################################################################### |
---|
74 | # Private interface. |
---|
75 | ########################################################################### |
---|
76 | |
---|
77 | def _compute_function(self, ary): |
---|
78 | "Fills in ary with the sin(r)/r function" |
---|
79 | |
---|
80 | width, height = ary.shape |
---|
81 | for i in range(width): |
---|
82 | for j in range(height): |
---|
83 | x = i - width / 2.0 |
---|
84 | x = x / (width/2.0) * 15 |
---|
85 | y = j - height / 2.0 |
---|
86 | y = y / (height/2.0) * 15 |
---|
87 | |
---|
88 | radius = sqrt(x*x + y*y) |
---|
89 | if radius == 0.0: |
---|
90 | ary[i,j] = 1 |
---|
91 | else: |
---|
92 | ary[i,j] = sin(radius) / radius |
---|
93 | |
---|
94 | return |
---|
95 | |
---|
96 | def main(): |
---|
97 | |
---|
98 | # Create the GUI (this does NOT start the GUI event loop). |
---|
99 | gui = GUI() |
---|
100 | |
---|
101 | # Screen size: |
---|
102 | screen_width = gui.system_metrics.screen_width or 1024 |
---|
103 | screen_height = gui.system_metrics.screen_height or 768 |
---|
104 | |
---|
105 | # Create and open the main window. |
---|
106 | window = ImagePlotApplicationWindow( title = "Plot" ) |
---|
107 | #window.plot_item = object |
---|
108 | window.size = ( 2 * screen_width / 3, 2 * screen_height / 3 ) |
---|
109 | window.open() |
---|
110 | |
---|
111 | # Start the GUI event loop. |
---|
112 | gui.start_event_loop() |
---|
113 | |
---|
114 | |
---|
115 | #=============================================================================== |
---|
116 | # Program start-up: |
---|
117 | #=============================================================================== |
---|
118 | |
---|
119 | if __name__ == '__main__': |
---|
120 | main() |
---|