############ ############ This program plots several functions in a single plot, and saves the plot as plot.eps ############ ############ Written by Paul Batzing as an example for FYS2140 students. ############ ############ ############ DO NOT CHANGE ############ from pylab import * # For plotting and math. ############ ############ END OF DO NOT CHANGE ############ ############ ############ Replace with the function you want to plot ############ ############ The function you want to plot def f(x): ######## You can break up the calculation in bits. fx1 = sin(x) fx2 = cos(x) fx3 = x**2 fx= (fx1*fx2)*fx3# OBS! THIS IS AN EXAMPLE, REPLACE WITH THE FUNCTION YOU WANT TO PLOT. return fx # This returns the value you defined in fx. ############ ############ End of function. ############ ############ ############ Replace with the inline function you want to plot ############ ############ It is possible to define a function in one line as well: def funct(x): return 2*(sin(x)*cos(x))*(x**2) ############ ############ End of function ############ ############ ############ Replace with the range you want to plot. ############ ############ Define the range you want to plot over: xmin = 0 xmax = 2*pi x = linspace(xmin, xmax,1000) ############ ############ End of range. ############ ############ ############ The actual plot commands. ############ ############ Plot the function. Here you use plotname.plot(xrange, function(xrange), label="what is this?"): plt.plot(x, f(x), label="One cool function") plt.plot(x, funct(x), label="Another cool function") ############ You can use f(x) and funct(x) as you would numbers: plt.plot(x, -funct(x), label="A third cool function") ############ Write into the plot what the lines mean, 1=upper left, 2=upper right, 3 = lower left and 4 = lower right: plt.legend(loc=2) ############ ############ End of plot commands. ############ ############ ############ ALWAYS label your axises!!!! ############ ############ In physics you must ALWAYS lable you axises: plt.xlabel("Position [m]") plt.ylabel("Probability of finding an electron here.") ############ ############ End of labeling. ############ ############ ############ Give the plot a title, it makes it feel good. ############ ############ And it helps to write over the plot what you plot plt.title("Where are all the electrons gone?") ############ ############ End of title giving. ############ ############ ############ DO NOT CHANGE ############ ############ Saves and shows the plot: savefig('plot.eps') plt.show() ############ ############ END OF DO NOT CHANGE ############