PHYSICS 2235

Lab 06

You are trying to plot T vs. L.
Run the following code (call it **firstplot.py**) .

#! /usr/local/bin/python3 import numpy as np import matplotlib.pyplot as plt # define T array T = np.array([0.35, 0.8, 1.25]) # define L array L = np.array([10, 20, 30]) #plot L vs. T plt.plot(T,L,'ro') #y label plt.ylabel('Length (cm)') #x label plt.xlabel(' T^{2}(Second^2)') #save the plot in a png file #plt.savefig('foo.png') plt.show()

Now suppose you don't like the default range that is showing. You can extend the x-axis and the y-axis range by adding

plt.axis([0, 1.5, 0, 35 ])

xmin = the new minimum in the x-axis range xmax = the new maximum in the x-axis range ymin = the new minimum in the y-axis range ymax = the new maximum in the y-axis range

When you are plotting data points it is customary to show the uncertainty in your measurement. This uncertainty is displayed with the error bars. The function

Lerr = np.array([1.5,1.3,1.4]) plt.errorbar(T, L, fmt='ro', label="data", xerr=0.001, yerr=Lerr, ecolor='black')

Suppose you are trying to plot the function

f(t) = t^2 \bold{vs.} \ t

How would you do that?

import numpy as np import matplotlib.pyplot as plt # Fill the array t from 0-5 with steps of 0.2. t = np.arange(0., 5., 0.2) # plot the x-axis as t and the y-axis t**2 #'bs'use blue square as markers plt.plot(t, t**2, 'bs') #y label plt.ylabel('t**2') #x label plt.xlabel(' t') plt.show()

In some cases you will need to save multiple figures **subplots** in one plot next to each other or on top of each other. This exercise is an example of how we do that.

import numpy as np import matplotlib.pyplot as plt #Define the function f(t) def f(t): return np.exp(-t) * np.cos(2*np.pi*t) #define t1,t2 arrays t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) #first subplot plt.subplot(3,1,1) plt.plot(t1, f(t1), 'bo') plt.ylabel('f(t)') plt.xlabel('t') #second subplot plt.subplot(3,1,2) plt.plot(t2, f(t2), 'k') plt.ylabel('f(t)') plt.xlabel('t') #third subplot plt.subplot(3,1,3) plt.plot(t1, f(t1), 'r--') plt.ylabel('f(t)') plt.xlabel('t') plt.show()

Create a plot **sinx.png** that contains three subplots. The subplots are.

1)

f1(x) = \frac{sin(x)}{x}

2) f2(x) = \frac{sin(x)}{x^2}

3) f3(x) = \frac{sin(x)}{x^3}

submit your code

f(x) = exp(-x^2)

for the range(-5 <x<5)

\frac{f(x+h)-f(x-h)}{2h}

for the range (-5 <x<5).
Plot both plots in the two subplots including the y-axis and x axis labels.

To calculate the derivative of f(x) assume a small step size (h = 0.001)

Create three arrays (t,d,dy) containing the following data points.

t d dy 1.0 2.94 0.7 4.5 8.29 1.2 8.0 9.36 1.2 11.5 11.60 1.4 15.0 9.32 1.3 18.5 7.75 1.1 22.0 8.06 1.2 25.5 5.60 1.0 29.0 4.50 0.8 32.5 4.01 0.8 36.0 2.62 0.7 39.5 1.70 0.6 43.0 2.03 0.6

submit the figure

Back to main Physics 2235 page