import math #The following is a course module that #gets you graphics and basic data handling #and numerical analysis routines from ClimateUtilities import * #Define the function you are going to plot. #It has a parameter, "a" def f(x,a): return math.exp(-x*x/(a*a)) #Make a list of the values of x you want to use xList = [-2. + .1*i for i in range(21)] #Make a list of the values of the function for two different #parameter values f1List = [f(x,1.) for x in xList] f2List = [f(x,2.) for x in xList] #Put the values in a Curve() object, for output and plotting #the Curve class is defined in ClimateUtilities c = Curve() c.addCurve(xList,'x','x axis') #Third argument is a label. It's optional c.addCurve(f1List,'f1','f(x,1)') c.addCurve(f2List,'f2','f(x,2)') #Dump the results out to a text file, so you can #plot it using your favorite plotting software if you like c.dump('fData.txt') #Argument is the file name #Now plot to the screen. The plot function returns a #"plot object", which we call "w" here. This allows the #plot to be saved later w = plot(c) #Now let's add a plot title and some axis labels and plot again. #Plot specifications are stored in the curve object #Now plot it again and see how we like it c.PlotTitle = 'The Gaussian' #Title of the plot c.Xlabel = 'The X axis' #X axis label c.Ylabel = 'Function Value' #Y axis label w1 = plot(c) #Now make the vertical axis logarithmic c.YlogAxis = True w2 = plot(c) #Save the plot as an EPS file, for use in a paper or report #Most good graphics programs can open an eps file, as well as #most word processors. Even the basic Preview software on the Mac #can open an eps and let you copy and paste it into another document # #Note that the plot (like the data in the c.dump(...) example) will #be saved on the computer that is running Python. If you are running #python remotely, via a terminal connection or x11, you will need to #move the file w2.save('myplot') #File will be saved as 'myplot.eps' #----------------Advanced Topic--------------------------------- # How to save the entire Curve object for future use # #In the c.dump(...) example above, only the data in the curve #object is written to a file. This is done deliberately, so that #the data will wind up in a plain-vanilla text file that can #be read and used by just about any software. However, this #file doesn't preserve the other things stored in the Curve object, #like plot titles and axis options. Python provides a means of preserving #an entire object for future use, known as "pickling." This process #is illustrated in the following example. # #First import the pickle module import pickle # #Now open a file for output. The open statement returns a file object outfile = open('testpickle.out','w') #The "w" means create new file for write # #Pickle the curve object and put it in the file pickle.dump(c,outfile) # #Close the file outfile.close() #Now let's open the file for input, read in the curve, and plot it infile = open('testpickle.out') newC = pickle.load(infile) plot(newC)