File: plplotcanvas_animation.py

package info (click to toggle)
plplot 5.10.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 26,280 kB
  • ctags: 13,512
  • sloc: ansic: 83,001; xml: 27,081; ada: 18,878; cpp: 15,966; tcl: 11,651; python: 7,075; f90: 7,058; ml: 6,974; java: 6,665; perl: 5,029; sh: 2,210; makefile: 199; lisp: 75; sed: 25; fortran: 7
file content (237 lines) | stat: -rwxr-xr-x 6,584 bytes parent folder | download | duplicates (2)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python
"""
animation.py - Demonstrates the use of the plplot canvas widget with gtk.

  Copyright (C) 2004, 2005 Thomas J. Duck
  All rights reserved.

  Thomas J. Duck <tom.duck@dal.ca>
  Department of Physics and Atmospheric Science,
  Dalhousie University, Halifax, Nova Scotia, Canada, B3H 3J5

  $Author: airwin $
  $Revision: 10568 $
  $Date: 2009-11-09 16:05:09 -0800 (Mon, 09 Nov 2009) $
  $Name$


NOTICE

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  MA  02110-1301  USA


DESCRIPTION

  This program demonstrates the use of the plplot canvas widget with gtk.
  Two graphs are draw in a window.  When the Execute button is pressed,
  two different waves progress through the graph in real time.  Plotting
  to the two graphs is handled in two different threads.
"""
import sys,threading
import plplot_python_start
import plplot_py_demos
import plplotcanvas
import gtk


# The number of time steps
STEPS = 100

# The number of points and period for the first wave
NPTS = 100
PERIOD = 30

# The width and height for each plot widget
WIDTH = 800
HEIGHT = 300

# Run the plots in different threads
thread0 = None
thread1 = None

# Create two different canvases
canvas0=None
canvas1=None

# Create the x array
x = plplot_py_demos.arange(NPTS)

# Lock on the gtkstate so that we don't try to plot after gtk_main_quit
GTKSTATE_CONTINUE=True
GTKSTATE_QUIT=False
gtk_state_lock = threading.Lock()
gtk_state = GTKSTATE_CONTINUE

# setup_plt - preparation for plotting an animation to a canvas
def setup_plot(canvas,title):
    # Set up the viewport and window
    canvas.vsta()
    canvas.wind(x[0],x[NPTS-1],-2.,2.)

    # Set the pen width
    canvas.wid(2)

    # The axes should be persistent, so that they don't have to be 
    # replotted every time (which would slow down the animation)
    canvas.use_persistence(True);

    # Draw the axes
    canvas.col0(15)
    canvas.box("bcnst",0.,0,"bcnstv",0.,0);
    canvas.lab("Phase","Amplitude",title);

    # Prepare for plotting
    canvas.col0(canvas.get_stream_number()+8)

    # The animated data should not be persistent
    canvas.use_persistence(False);

# plot - draws a plot on a canvas
def plot(canvas,offset,title):

    global x

    # Get the stream number
    Nstream = canvas.get_stream_number()

    # Generate the sinusoid
    y = plplot_py_demos.sin(2.*3.14*(x+offset*(Nstream+1))/PERIOD/(Nstream+1))
    
    # Draw the line
    canvas.line(x, y)

    # Advance the page
    canvas.adv(0)

# Delete event callback
def delete_event(widget, event, data=None):
    return False

# Destroy event calback
def destroy(widget, data=None):

    global gtk_state
    
    gtk_state_lock.acquire()
    gtk_state = GTKSTATE_QUIT
    gtk_state_lock.release()

    gtk.main_quit()
    
def plot_thread(canvas,title):

    # Draw plots in succession
    for i in range(STEPS):
        gtk.gdk.threads_enter()

        # Lock the current gtk state
        gtk_state_lock.acquire()

        # Check to make sure gtk hasn't quit
        if gtk_state == GTKSTATE_QUIT:
            gtk_state_lock.release()
            gtk.gdk.threads_leave()
            return

        # Draw the plot
        plot(canvas,i,title)

        # Release the lock
        gtk_state_lock.release()
        gtk.gdk.threads_leave()

# Start threads callback from execute button
def start_threads(widget,data):

    global thread0
    global thread1

    # Ignore call if threads are currently active
    if (thread0!=None or thread1!=None) and \
       (thread0.isAlive() or thread1.isAlive()): return
    
    # Create the two plotting threads
    thread0 = threading.Thread(None,plot_thread,
                               kwargs={"canvas":canvas0,
                               "title":"A phase-progressing wave"})
    thread0.start()

    thread1 = threading.Thread(None,plot_thread,
                               kwargs={"canvas":canvas1,
                               "title":"Another phase-progressing wave"})
    thread1.start()
  
if __name__ == "__main__":

    # Parse the options
    plplot_py_demos.plparseopts(sys.argv,plplot_py_demos.PL_PARSE_FULL);

    # Initialize
    gtk.gdk.threads_init()

    # Create the first canvas, set its size, draw some axes on it, and
    # place it in a frame
    canvas0=plplotcanvas.Canvas()
    canvas0.set_size(WIDTH,HEIGHT)
    canvas0.adv(0)  # Advance the page to finalize the plot
    setup_plot(canvas0,"A phase-progressing wave")
    canvas0frame=gtk.Frame()
    canvas0frame.set_shadow_type(type=gtk.SHADOW_ETCHED_OUT)
    canvas0frame.add(canvas0)

    # Create the second canvas, set its size, draw some axes on it, and
    # place it in a frame
    canvas1=plplotcanvas.Canvas()
    canvas1.set_size(WIDTH,HEIGHT)
    canvas1.adv(0)  # Advance the page to finalize the plot
    setup_plot(canvas1,"Another phase-progressing wave")
    canvas1frame=gtk.Frame()
    canvas1frame.set_shadow_type(type=gtk.SHADOW_ETCHED_OUT)
    canvas1frame.add(canvas1)

    # Create a button and put it in a box
    button = gtk.Button (stock=gtk.STOCK_EXECUTE);
    button.connect("clicked", start_threads, None)   
    button.set_border_width(10)
    buttonbox = gtk.HBox()
    buttonbox.pack_start(button,True,False,0)
    
    # Create and fill the vbox with the widgets
    vbox = gtk.VBox()
    vbox.pack_start(canvas0frame,True,False,0)
    vbox.pack_start(canvas1frame,True,False,10)
    vbox.pack_start(buttonbox,True,False,0)
        
    # Create a new window
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    
    # Set the border width of the window
    window.set_border_width(10)

    # Connect the signal handlers to the window decorations
    window.connect("delete_event",delete_event)
    window.connect("destroy",destroy)

    # Put the vbox into the window
    window.add(vbox)
    
    # Display everything
    window.show_all()

    # Start the gtk main loop
    gtk.gdk.threads_enter()
    gtk.main()
    gtk.gdk.threads_leave()