1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
#!/usr/bin/env python
"""
A small test app that uses FloatCanvas to draw a vector plot.
"""
import wx
import numpy as np
import random
## import the installed version
from wx.lib.floatcanvas import NavCanvas, FloatCanvas
## import a local version
#import sys
#sys.path.append("../")
#from floatcanvas import NavCanvas, FloatCanvas
class DrawFrame(wx.Frame):
def __init__(self,parent, id,title,position,size):
wx.Frame.__init__(self,parent, id,title,position, size)
## Set up the MenuBar
MenuBar = wx.MenuBar()
file_menu = wx.Menu()
item = file_menu.Append(wx.ID_ANY, "E&xit","Terminate the program")
self.Bind(wx.EVT_MENU, self.OnQuit, item)
MenuBar.Append(file_menu, "&File")
draw_menu = wx.Menu()
item = draw_menu.Append(wx.ID_ANY, "&Plot","Re-do Plot")
self.Bind(wx.EVT_MENU, self.Plot, item)
MenuBar.Append(draw_menu, "&Plot")
help_menu = wx.Menu()
item = help_menu.Append(wx.ID_ANY, "&About",
"More information About this program")
self.Bind(wx.EVT_MENU, self.OnAbout, item)
MenuBar.Append(help_menu, "&Help")
self.SetMenuBar(MenuBar)
self.CreateStatusBar()
self.SetStatusText("")
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
# Add the Canvas
self.Canvas = NavCanvas.NavCanvas(self ,wx.ID_ANY ,(500,300),
ProjectionFun = None,
Debug = 0,
BackgroundColor = "WHITE"
).Canvas
self.Canvas.NumBetweenBlits = 1000
self.Show(True)
self.Plot()
return None
def OnAbout(self, event):
dlg = wx.MessageDialog(self, "This is a small program to demonstrate\n"
"the use of the FloatCanvas\n"
"for vector plotting",
"About Me", wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def ZoomToFit(self,event):
self.Canvas.ZoomToBB()
def OnQuit(self,event):
self.Close(True)
def OnCloseWindow(self, event):
self.Destroy()
def DrawAxis(self):
Canvas = self.Canvas
# Draw the Axis
# Note: the AddRectangle Parameters all have sensible
# defaults. I've put them all here explicitly, so you can see
# what the options are.
self.Canvas.AddRectangle((0, -1.1),
(2*np.pi, 2.2),
LineColor = "Black",
LineStyle = "Solid",
LineWidth = 1,
FillColor = None,
FillStyle = "Solid",
InForeground = 0)
for tic in np.arange(7):
self.Canvas.AddText("%1.1f"%tic,
(tic, -1.1),
Position = 'tc')
for tic in np.arange(-1,1.1,0.5):
self.Canvas.AddText("%1.1f"%tic,
(0,tic),
Position = 'cr')
# Add a phantom rectangle to get the bounding box right
# (the bounding box doesn't get unscaled text right)
self.Canvas.AddRectangle((-0.7, -1.5),
(7, 3),
LineColor = None)
#Canvas.ZoomToBB()
#Canvas.Draw()
def Plot(self, event = None):
x = np.arange(0, 2*np.pi, 0.1)
x.shape = (-1,1)
y = np.sin(x)
data = np.concatenate((x, y),1)
Canvas = self.Canvas
self.Canvas.ClearAll()
self.DrawAxis()
for p in data:
Canvas.AddPoint(p,
Diameter = 4,
Color = "Red",
InForeground = 1)
theta = random.uniform(0, 360)
Canvas.AddArrow(p,
Length = 20,
Direction = p[1]*360,
LineColor = "Red",
)
self.Canvas.ZoomToBB()
self.Canvas.Draw()
self.Canvas.SaveAsImage("junk.png")
class DemoApp(wx.App):
"""
How the demo works:
Either under the Draw menu, or on the toolbar, you can push Run and Stop
"Run" start an oscilloscope like display of a moving sine curve
"Stop" stops it.
While the plot os running (or not) you can zoom in and out and move
about the picture. There is a tool bar with three tools that can be
selected.
The magnifying glass with the plus is the zoom in tool. Once selected,
if you click the image, it will zoom in, centered on where you
clicked. If you click and drag the mouse, you will get a rubber band
box, and the image will zoom to fit that box when you release it.
The magnifying glass with the minus is the zoom out tool. Once selected,
if you click the image, it will zoom out, centered on where you
clicked.
The hand is the move tool. Once selected, if you click and drag on
the image, it will move so that the part you clicked on ends up
where you release the mouse. Nothing is changed while you are
dragging, but you can see the outline of the former picture.
I'd like the cursor to change as you change tools, but the stock
wx.Cursors didn't include anything I liked, so I stuck with the
pointer. Please let me know if you have any nice cursor images for me to
use.
Any bugs, comments, feedback, questions, and especially code are welcome:
-Chris Barker
Chris.Barker@noaa.gov
"""
def OnInit(self):
frame = DrawFrame(None, wx.ID_ANY, "Plotting Test",wx.DefaultPosition,wx.Size(700,400))
self.SetTopWindow(frame)
return True
if __name__ == "__main__":
app = DemoApp(0)
app.MainLoop()
|