import pylab as pl #Note: Comment out everything but the code number you are working on. The pl.show() #function hangs on some OSs so you won't be able to run everything at once. ####CODE 1. Simple example, outputs a figure ##pl.figure(1) #Creates figure 1 ##pl.plot([1,2,3,4], [1,7,3,5]) #draw on figure 1 ##pl.show() #This may suspend the Python process until the figure is closed, which is why it should always come last ####END OF CODE 1 ####CODE 2. Creating multiple plots and saving them ##pl.figure(1) #Create first figure ##pl.plot([1,2,3,4], [1,7,3,5]) #Draw on first figure ## ##pl.figure(2) #Open up another figure ##pl.plot([5.5,4,3,2], [1,2,3,4]) #Draw on second figure. ##pl.savefig('plot-2') #Save second figure (it is the 'current' figure) ## ##pl.figure(1) #Switch focus back to figure 1 ##pl.plot([2,3,10,5]) #Draw something else on figure 1. Only y values; x values default to 0...3 ##pl.savefig('plot-1') #Saves plot 1 ####Open up files in folder. They should have been saved as .png files. ####END OF CODE 2 ####CODE 3. Use subplots instead of plotting on the same axes ##def f(t): ## return pl.exp(-t) * pl.cos(2*pl.pi*t) ## ##t1 = pl.arange(0.0, 5.0, 0.1) ##t2 = pl.arange(0.0, 5.0, 0.02) ## ##pl.figure(1) ##pl.subplot(2,1,1) #Draw plot at the first position in a grid of 2 rows and 1 column ##pl.plot(t1, f(t1)) ## ##pl.subplot(2,1,2) #Draw plot at the second position in a grid of 2 rows and 1 column ##pl.plot(t2, pl.cos(2*pl.pi*t2)) ##pl.show() ####END OF CODE 3 ####CODE 4.Plotting a sine wave using line formatting, title, labels, axis limits, legend ##x = pl.linspace(0,20,100) #Get 100 evenly spaced values from 0 to 20 (x is an array) ##sinx = pl.sin(x) ## ##pl.figure(1) ## ###Notice the label keyword in plot. The label will be used later when a legend is created. ##pl.plot(x,sinx, label = 'sine') ## ###Show effect of changing linewidth by uncommenting this: ###pl.plot(x,sinx, label = 'sine', linewidth = 30); ## ###Add titles, axis labels (THIS SHOULD ALWAYS BE DONE WITH PLOTS) ##pl.title('Sine curve') ##pl.xlabel('x') ##pl.ylabel('value') ## ###Show effect of changing fontsize by uncommenting this: ###pl.ylabel('sinx', fontsize = 20) ## ###Show how limits work using these lines: ###pl.xlim(5,15) ###pl.ylim(0,1) ## ## ###Plot a cosine wave with the format string 'ro', indicating a red marker with circles ###for the points. The default for plot is 'b-', indicating a blue solid line. ###These letters and symbols are derived from MATLAB rules for formatting. ###For complete list, see ###matplotlib.sourceforge.net/api/pylot_api.html#matplotlib.pyplot.plot ## ##cosx = pl.cos(x) ##pl.plot(x,cosx, 'ro', label = 'cosine') ##pl.legend(loc='upper right') ##pl.title('Sine and Cosine Curves') ## ##pl.show() ####END OF CODE 4 ####CODE 5: Plotting distributions. Coin tossing example. 1000 trials, 100 tosses ###each trial. This code plots the distribution with a histogram and also uses text annotation. ##import random ##vals = pl.zeros(1000) ## ##for trial in range(1000): ## numHeads = 0 ## for flips in range(100): ## if(random.random() > 0.5): ## numHeads += 1 ## vals[trial] = numHeads/100.0 ## ##mn = pl.mean(vals) ##sd = pl.std(vals) ## ##pl.hist(vals,bins = 10) ##pl.xlim(0.2,0.8) ##pl.title('Coin Tossing Results') ##pl.xlabel('Fraction of Heads') ##pl.ylabel('Number of trials') ## ##xmin,xmax = pl.xlim() ##ymin,ymax = pl.ylim() ## ###Example of using the text function to display the mean and std. deviation ###The first argument is the x location, second argument is y location, third ###is the actual text ##pl.text(xmin + (xmax-xmin) * 0.02, ymin + (ymax-ymin) * 0.8, ## 'Mean =' + str(round(mn,4)) + ", Std = " + str(round(sd,4))) ## ##pl.show() ####END OF CODE 5