PHYSICS 2235

Lab 07

In some cases you will need to plot multiple data points on the same plot

import numpy as np import matplotlib.pyplot as plt def f1(t): return t def f2(t): return t**2 def f3(t): return t**3 t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangle plt.plot(t, t, 'r--') plt.plot(t,f2(t), 'bs') plt.plot(t,f3(t), 'g^') plt.ylabel('functions of t') plt.xlabel('t') plt.title('Some Functions') plt.show()Now which plot is which function?

To be able to to save that information on the plot you need legends.

To have legends on your plot replace the lines in the previous code

plt.plot(t, t, 'r--') plt.plot(t,f2(t), 'bs') plt.plot(t,f3(t), 'g^')with

# red dashes, blue squares and green triangles plt.plot(t, t, 'r--', label='linear') plt.plot(t,f2(t), 'bs', label='to the power 2') plt.plot(t,f3(t), 'go', label='to the power 3') plt.legend()

your data contains a wide range of numbers from less than unity to several thounsands.

import numpy as np import matplotlib.pyplot as plt tau = 20 N0=8200 t = np.linspace(0, 180, 128) N = N0 * np.exp(-t/tau) plt.semilogy(t, N, 'bs') plt.ylabel('log(N)') plt.xlabel(' t') plt.show()

make another subplot without the log-scale. (using plot). How does the two compare?

Suppose you are trying to plot the following columns.

time (s) position (m) uncertainty (m) 0.0 0.0 0.04 0.5 1.3 0.12 1.0 5.1 0.2 1.5 10.9 0.3 2.0 18.9 0.4 2.5 28.7 0.4 3.0 40.3 0.5 3.5 53.1 0.6 4.0 67.5 0.6 4.5 82.3 0.6 5.0 97.6 0.7 5.5 113.8 0.7 6.0 131.2 0.7 6.5 148.5 0.7 7.0 166.2 0.7 7.5 184.2 0.7 8.0 201.6 0.7 8.5 220.1 0.7 9.0 238.3 0.7 9.5 256.5 0.7 10.0 275.6 0.8You probably noticed that feeding these columns by hand to arrays is quite limited.

The best way to do this is to create a data file that contain the data points.

Simply copy and paste the following lines in a data file called

import numpy as np t, d, dy = np.loadtxt("MotionData.txt", skiprows=1 , unpack=True)explaining loadtxt command

A useful command you could use if you don't want to read your whole file. In this case only the first two columns.

import numpy as np x, y = np.loadtxt("MotionData.txt", skiprows=1 ,usecols = (0,1), unpack=True)explaining more on loadtxt command options

suppose the data you are using is saved in an Excel sheet.

data point,time (sec),height (mm),uncertainty (mm) 0,0,180,3.5 1,0.5,182,4.5 2,1,178,4 3,1.5,165,5.5 4,2,160,2.5 5,2.5,148,3 6,3,136,2.5 7,3.5,120,3 8,4,99,4 9,4.5,83,2.5 10,5,55,3.6 11,5.5,35,1.75 12,6,5,0.75

copy and paste this file into a file called

Note that since the separator between columns here is NOT white space or tab you would need the following command.

import numpy as np t, h = np.loadtxt("MyExcel.csv", skiprows=1 ,usecols = (1,2),unpack=True, delimiter=',')which contains the delimiter option to ',' that separates the columns in this file. Try it.

savetxt(filename, array, fmt="%0.18e", delimiter=" ", newline="\n", header="", footer="", comments="# ")For example:

import numpy as np x, y, err = np.loadtxt("MotionData.txt", skiprows=1 , unpack=True) np.savetxt('MotionDataOut.txt',np.c_[x,y], fmt='%1.1f' )explaining line

A simple harmonic oscillator consists of a block with mass m. A general solution for the motion of the block is given by.

x(t) = C cos(wt + \phi)

where C is the amplitude and phi is the phase. Create a plot

Plot a(x) = C*Cos(Wx+Phi) vs. x[0-2*pi] C,W,Phi (1.0 , 1.0, 0) Plot b(x) = C*Cos(Wx+Phi) vs. x[0-2*pi] C,W,Phi (2.0 , 1.0, 0) Plot c(x) = C*Cos(Wx+Phi) vs. x[0-2*pi] C,W,Phi (3.0 , 1.0, 0)

plot d(x) = C*Cos(Wx+Phi) vs. x[0-2*pi] C,W,Phi (1.0 , 1.0, 0) plot e(x) = C*Cos(Wx+Phi) vs. x[0-2*pi] C,W,Phi (1.0 , 1.0, pi) plot f(x) = C*Cos(Wx+Phi) vs. x[0-2*pi] C,W,Phi (1.0 , 1.0, pi/2)

submit your code

P(x;\mu;\sigma) = \frac{1}{\sqrt{2 \pi \sigma^{2}}} e^{\frac{-(x-\mu)^2}{2\sigma^2}}

where p is the probability of x, mu is the average and sigma is the standard deviation.

On the same plot as the data you just plotted.

submit your code and the plot as

Back to main Physics 2235 page