PHYSICS 2235

PHYSICS 2235 Lab 06

Plotting is an important visualization tool. In the following examples we will use matplotlib to visualize function, data..etc

Exercise 1: Plotting data points

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()



• code explanation
• Symbol type and color

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 ])

• plt.axis([xmin, xmax, ymin, ymax ])
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

• Change the x and y axis range in firstplot.py using the line provided.
• Again, try to change the x range to go from 0 to 3 and y axis ranges to go from 0 to 100.

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 errorbar plots data with error bars attached.

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

• errorbar explanation

Exercise 2: Plotting Functions

We tried to plot data points. What about functions.

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()


• Change the x axis range from 0 to 2.
• Change the y axis range from -2 to 2
• Now try to to plot cos(x) vs. x [0-2*Pi].

Exercise 3: Multiple plots

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.

Plotting Multiple Functions:
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()


• code explanation

Problem 1

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}
 Define a function that calculates sin(x)/x**n
for each subplot plot fi(x) vs. x
The range for all the subplots is from (-5<x<5)
Draw the title for each subplot
Draw the y-axis label for each subplot
Draw the x-axis label for the last subplot
use a different color or marker for each subplot



• Hint: Note that fi(x) is a function of both x and n. Here n is the power of x. We did a similar example in problem 2 lab03.

Problem 2

Create a plot derivx.png that contains two subplots. The subplots are.

• Plot the function
f(x) = exp(-x^2)

for the range(-5 <x<5)

• Draw the derivative of f(x) using the formula
\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)
• Hint: Define x as an array (include enough points that f(x) appear smooth)
• Hint: Use a for-loop to calculate the derivative.

Problem 3

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

• plot t on the x-axis and d on the y-axis.
• plot the errors in the t to be 0.001.
• plot the errors on the d to be dy.
• use red circles for the data points.
• use blue lines for the error bars
• label the x-axis as time
• label the y-axis as y(t)

submit the figure motion.png and the python file motion.py

Back to main Physics 2235 page