%matplotlib notebook # JR 6/21/14 # Reimplementation of my java phase plane draw in python/pylab. # Idea from Interactive Differential Equations. # Works pretty well. # Don't see how to get the t curves to keep drawing # when pointer stationary and hence not generating events. # Not a major problem, though. from pylab import * from time import time import numpy as np tmax = 20. # in seconds. Time starts on first click. nt = 10 # number of t-grid intervals xycurves = {} xtcurves = {} tycurves = {} figure(figsize=(8,8)) xyax = axes([ 0,.5,.5,.5])#,aspect=1) xtax = axes([ 0, 0,.5,.5]) tyax = axes([.5,.5,.5,.5]) def move(event): global tstart,x,y,t,xycurves,tycurves,xyinv if buttonisdown: ux,uy = xyinv.transform( (event.x,event.y) ) x.append(ux) y.append(uy) t.append( (time() - tstart) ) xycurves[ncurves].set_xdata(x) xycurves[ncurves].set_ydata(y) tycurves[ncurves].set_xdata(t) tycurves[ncurves].set_ydata(y) xtcurves[ncurves].set_xdata(x) xtcurves[ncurves].set_ydata(t) draw() def press(event): global started,tstart,buttonisdown,x,y,t,xycurves,tycurves,xyinv xyinv = xyax.transData.inverted() # function to convert from pixels to user coordinates ux,uy = xyinv.transform( (event.x,event.y) ) x = [ux] y = [uy] if not started: tstart = time() started = True eventt = time() - tstart t = [eventt] xycurves[ncurves], = xyax.plot(x,y) tycurves[ncurves], = tyax.plot(t,y) xtcurves[ncurves], = xtax.plot(x,t) buttonisdown = True def release(event): global buttonisdown,ncurves xyinv = xyax.transData.inverted() # function to convert from pixels to user coordinates move(event) # to get last point drawn ncurves += 1 buttonisdown = False connect('motion_notify_event' , move ) connect('button_press_event' , press ) connect('button_release_event', release) started = False buttonisdown = False ncurves = 0 alpha = 0.9 xyax.plot( [-1, 1], [ 0, 0], 'm', alpha=alpha); xyax.text( .93 , -.08, 'x', color='m') xyax.plot( [ 0, 0], [-1, 1], 'b', alpha=alpha); xyax.text( .02 , .9 , 'y', color='b') tyax.plot( [0,tmax], [ 0, 0], 'k', alpha=alpha); tyax.text(tmax*.97 , -.08, 't', color='k') tyax.plot( [ 0, 0], [-1, 1], 'b', alpha=alpha); tyax.text(tmax*.01 , .9 , 'y', color='b') xtax.plot( [0, 0], [ 0,tmax], 'k', alpha=alpha); xtax.text( .02 , tmax*.99, 't', color='k') xtax.plot( [-1, 1], [ 0, 0], 'm', alpha=alpha); xtax.text( .93 , tmax*.04, 'x', color='m') for t in np.linspace(0,tmax,nt+1): tyax.plot([ t,t],[-1,1],'b',alpha=0.2) xtax.plot([-1,1],[ t,t],'m',alpha=0.2) xyax.set_xlim(-1,1) xyax.set_ylim(-1,1) xyinv = xyax.transData.inverted() # function to convert from pixels to user coordinates tyax.set_xlim(0,tmax) tyax.set_ylim(-1,1) xtax.set_xlim(-1,1) xtax.set_ylim(tmax,0) xyax.set_xticks([]) xyax.set_yticks([]) tyax.set_xticks([]) tyax.set_yticks([]) xtax.set_xticks([]) xtax.set_yticks([]) print("Draw in upper-left panel")