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