lec5_PylabIntro

# lec5_PylabIntro -   Scatter plot – styles and options,...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview:   Scatter plot – styles and options, subplots, dual axes, decorations and annotations, text inclusions   Other types of plots – polar axes, vector plots   Interacting with plots, animations (not working yet!) Class Notes   Output formats – bitmap vs vector   Hand in HW#1 hard copy and email the code   Pick up HW#2   You should be reading in Chap. 4   Download Pylab manual from my website and look over it.   Generally in scientific work we use scatter plots. Other types are bar charts, bubble chart, …   plot command allows for many types of marker and line types and colors. Format is: plot(x,y,’g-o’). Type ‘help(plot)’ to see all the options from pylab import * t=linspace(0,10,100) RC=2.0 V=10*exp(-t/RC) plot(t,V, r-s ,label= Circuit 1 ) legend(loc=1) xlabel( Time (s) , fontsize=16) ylabel( Potential (V) ,fontsize=16) show()   Can plot multiple curves on a graph by repeatedly calling plot commands.   ‘label’ / legend() command labels each curve from pylab import * t=linspace(0,10,100) def V(t,RC): return 10*exp(-t/RC) RCvals=[0.5,1.0,2.0,4.0] markers=['o','s','^','+','D'] for i in range( len(RCvals) ): marker=markers[i] RC=RCvals[i] plot(t,V(t,RC),'b-'+marker, \ label='RC=%2.1f'%RC) legend(loc=1) xlabel('Time (s)', fontsize=16) ylabel('Potential (V)',fontsize=16) show()   Sometimes one curve has a very different scale than the other curves.   Add a 2nd y-axis (on the right) with a different scale. Use the ‘twinx()’ function (yes ‘x’, not ‘y’) x=linspace(0,10,100) y1=sqrt(x) y2=x**3 plot(x,y1,'g-',lw=2,label='Sqrt(x)') ylabel('Sqrt(x)') legend(loc=2) ax2=twinx() plot(x,y2,'b--',lw=2,label='x^3') ylabel('x^3') xlabel('x') legend(loc=4)   Add several separate graphs to one Figure frame.   Use: subplot(columns,rows,currentplot) x=linspace(-np.pi,np.pi,100) subplot(221) plot(x,sin(x)) xlabel('X') ylabel('Sin(x)') subplot(222) plot(x,cos(x)) xlabel('X') ylabel('Cos(x)') subplot(223) plot(x,tan(x)) xlabel('X') ylabel('Tan(x)') subplot(224) plot(x,cosh(x)) xlabel('X') ylabel('Cosh(x)')   Can annotate with arrows and text: text(xfig,yfig,string) and annotate(‘text’,xy=(xpos,ypos), xytext=(xpos,ypos), arrowprops=dict(..)) x=linspace(0,np.pi,100) plot(x,tan(x),'g-',lw=2) annotate('A pole',xy=(np.pi/2.0,50), xytext=(0.25,10), fontsize=16, arrowprops=dict(facecolor='blue',shrink=0.05)) text(2,40,'$\int_0^\pi \mathrm{tan}(x) = 0$',fontsize=25)   Add a second figure frame for a new plot in the same script.   figure(2) or figure(2,figsize=(8,6), facecolor=‘lightgoldenrodyellow’,edgecolor=‘blue’)   Polar plots can be created with: polar(theta,r)   Other style attributes (like lw, color, ms,…) still apply.   Vectors have both magnitude and direction.  On an x,y grid, the field will have both and x and y component. Use ‘meshgrid’ to create the 2D x,y grid.   quiver plot will plot the direction and magnitude of the field at each point. x,y=meshgrid(linspace (0,2*np.pi,30),linspace (0,2*np.pi,30)) U=sin(x) V=cos(y) quiver(x,y,U,V, sqrt(U**2+V**2)) title('Vector (quiver) plot of V_x=sin(x), V_y=cos(y)', fontsize=20) xlabel('x',fontsize=20) ylabel('y',fontsize=20)   For interacting, we need to look for ‘events’ like clicking or pressing a key and do something in response.  This is called binding events to a function and is done with the ‘connect’ function.   Types of events can be:  ’key_press_event’: returns what ever key pressed   ‘button_press_event’: returns the x and y coordinates of the location clicked and which button.   Several other types of events: see ‘help connect’   Here’s an example: plot a gaussian peaked where the user clicks. Make each new curve a random color and let user clear the curves with a ‘c’ press and quit with a ‘q’ press. def click(event): col=(rand(),rand(),rand()) xClick,yClick= event.xdata, event.ydata plot(x,gauss(x,A=yClick,x0=xClick),color=col) draw() def done(event1): if event1.key == 'q': close('all') if event1.key == 'c': clf() plot([0],[0] axis([0, 10, 0, 1]) draw() #now register the event to the above function cid = connect('button_press_event',click) cid1= connect('key_press_event',done) title("Click anywhere in the graph to draw a new Gaussian.\ \n Type 'q' to quit or 'c' to clear graph") xlabel("X") ylabel("Y") plot([0],[0]) #clears existing graphs axis([min(x), max(x), 0, 1]) show() ...
View Full Document

Ask a homework question - tutors are online