20
1 The First Few Steps
t = np.linspace(0, 1, 1001)
y = v0*t - 0.5*g*t**2
plt.plot(t, y)
# plots all y coordinates vs. all t coordinates
plt.xlabel(’t (s)’)
# places the text t (s) on x-axis
plt.ylabel(’y (m)’)
# places the text y (m) on y-axis
plt.show()
# displays the figure
This program produces a plot of the vertical position with time, as seen in
Fig. 1.1. As you notice, the code lines from the ball.py program in Sect. 1.2 have
not changed much, but the height is now computed and plotted for a thousand points
in time!
Let us take a closer look at this program. At the top, we recognize the import
statements
import numpy as np
import matplotlib.pyplot as plt
As we know by now, these statements imply that items from numpy and
matplotlib.pyplot must be prefixed with np and plt, respectively.
The linspace Function Next, there is a call to the function linspace from the
numpy library. When n evenly spaced floating point numbers are sought on an
interval [a, b], linspace may generally be called like this:
np.linspace(a, b, n)
This means that the call
t = np.linspace(0, 1, 1001)
creates 1001 coordinates between 0 and 1, inclusive at both ends. The mathematically inclined reader might agree that 1001 coordinates correspond to 1000 equalFig. 1.1 Plot generated by the script ball_plot.py showing the vertical position of the ball
(computed for a thousand points in time)
1 The First Few Steps
t = np.linspace(0, 1, 1001)
y = v0*t - 0.5*g*t**2
plt.plot(t, y)
# plots all y coordinates vs. all t coordinates
plt.xlabel(’t (s)’)
# places the text t (s) on x-axis
plt.ylabel(’y (m)’)
# places the text y (m) on y-axis
plt.show()
# displays the figure
This program produces a plot of the vertical position with time, as seen in
Fig. 1.1. As you notice, the code lines from the ball.py program in Sect. 1.2 have
not changed much, but the height is now computed and plotted for a thousand points
in time!
Let us take a closer look at this program. At the top, we recognize the import
statements
import numpy as np
import matplotlib.pyplot as plt
As we know by now, these statements imply that items from numpy and
matplotlib.pyplot must be prefixed with np and plt, respectively.
The linspace Function Next, there is a call to the function linspace from the
numpy library. When n evenly spaced floating point numbers are sought on an
interval [a, b], linspace may generally be called like this:
np.linspace(a, b, n)
This means that the call
t = np.linspace(0, 1, 1001)
creates 1001 coordinates between 0 and 1, inclusive at both ends. The mathematically inclined reader might agree that 1001 coordinates correspond to 1000 equalFig. 1.1 Plot generated by the script ball_plot.py showing the vertical position of the ball
(computed for a thousand points in time)
