Wednesday, February 25th, 2026¶

Last week, we worked on documenting our code, learned how to use modules, and how to use the zip function.

In [1]:
def y(t, y0, v0=0, g=-9.8):
    return (g/2)*(t**2) + v0*t + y0

Given this function, it would be nice if we had a way to visualize the paths taken by objects in freefall. This leads us to...

Plotting in Python¶

We will the pyplot submodule from the matplotlib module for our plotting needs. When importing modules (or submodules), we can assign our own short-hand name using the syntax import <some module or submodule> as <some short-hand name>.

Typically, we will import the matplotlib.pyplot module and assign it the short-hand name plt.

In [2]:
import matplotlib.pyplot as plt
In [3]:
#help(plt)

The pyplot module has many for data visualization and plotting. The most basic is the plot function.

In [4]:
#help(plt.plot)

The plot function is extremely flexible in how it can be called (by making use of default arguments and keyword arguments). One of the most basic ways to use the plot function is to enter a list of $x$-values and a list of $y$-values as inputs. That is, plt.plot(<list of x-values>, <list of y-values>).

In [5]:
x_list = [ i for i in range(10) ]
y_list = [ x**2 for x in x_list ]

plt.plot(x_list, y_list)
Out[5]:
[<matplotlib.lines.Line2D at 0x1cb5268da90>]
No description has been provided for this image

Exercise: Use the function y defined previously to plot the height over time of an object dropped (from rest) from a height of $1,000$ meters (on Earth) for the range $0 \leq t \leq 16$.

In [11]:
t_list = [ t for t in range(17) ]
y_list = [ y(t,1000) for t in t_list ]
In [12]:
plt.plot(t_list, y_list)
Out[12]:
[<matplotlib.lines.Line2D at 0x1cb52822350>]
No description has been provided for this image

We can call plt.plot several times within a cell to plot several pieces of data.

Exercise: Replicate the previous plot, then add another curve that shows the height over time of the same object dropped on Mars, where the acceleration due to gravity is approximately $-3.71\, m/s^2$.

In [21]:
t_list = [ t for t in range(17) ]
y_Earth_list = [ y(t,1000) for t in t_list ]
y_Mars_list = [ y(t,1000, g=-3.71) for t in t_list ]
In [28]:
plt.plot(t_list, y_Earth_list)
plt.plot(t_list, y_Mars_list)
Out[28]:
[<matplotlib.lines.Line2D at 0x1cb5a652fd0>]
No description has been provided for this image

Options when calling plt.plot¶

By default, the plot function will connect the supplied data points with a polygonal line, with the color selected automatically. We can include additional keyword arguments to change this behavior.

Changing the color¶

For example, the keyword argument color can be used to manually set the color. There are many color characters available:

  • 'r': red
  • 'g': green
  • 'b': blue
  • 'k': black
  • 'm': magenta
  • 'c': cyan
  • 'y': yellow
  • 'C0', 'C1', ... 'C9': a sequence of colors that pyplot automatically uses when no color is supplied

We can also supply certain color names (see Matplotlib documentation), or an RGB triple of integers (we will discuss this later in the course).

Exercise: Replicate the previous plot, then modify the code so that the path of the object on Earth is plotted in blue and the path of the object on Mars is plotted in red.

In [29]:
plt.plot(t_list, y_Earth_list,color='b')
plt.plot(t_list, y_Mars_list,color='r')
Out[29]:
[<matplotlib.lines.Line2D at 0x1cb5a6ded50>]
No description has been provided for this image

Changing the marker style¶

The keyword argument marker can be used to change the marker style. Here are some of the available styles:

  • '.': point
  • ',': pixel
  • 'o': circle
  • '^': triangle
  • '*': star
  • 's': square
  • '+': plus
  • 'x': x

More can be found in the Matplotlib documentation.

Exercise: Replicate the previous plot, then modify the code so that the Earth path uses triangle markers and the Mars path uses square markers.

In [32]:
plt.plot(t_list, y_Earth_list,color='b', marker='^')
plt.plot(t_list, y_Mars_list,color='r', marker='s')
Out[32]:
[<matplotlib.lines.Line2D at 0x1cb5a7d6ad0>]
No description has been provided for this image

Changing the line style¶

The keyword argument linestyle can be used to change the line style. Here are some of the available styles:

  • '-' or 'solid': solid line
  • '--' or 'dashed': dashed line
  • ':' or 'dotted': dotted line
  • '-.' or 'dashdot': alternating dots and dashes
  • '' or 'None': no line

More can be found in the Matplotlib documentation.

Exercise: Replicate the previous plot, then modify the code so that the Earth path uses a dashed line and the Mars path uses a dotted line.

In [34]:
plt.plot(t_list, y_Earth_list,color='b', marker='^', linestyle='--')
plt.plot(t_list, y_Mars_list,color='r', marker='s', linestyle=':')
Out[34]:
[<matplotlib.lines.Line2D at 0x1cb5a8ea710>]
No description has been provided for this image

Using the fmt string short-hand¶

When calling plt.plot, we can include a string after our $x$- and $y$-values with color, marker, and line configuration options. For example, the calling plt.plot(x_list, y_list, 'r*--') tells plt.plot to plot in red with stars for markers and dashed lines.

Exercise: Replicate the previous plot using the fmt string instead of the color, marker, and linestyle keyword arguments.

In [35]:
plt.plot(t_list, y_Earth_list,'b^--')
plt.plot(t_list, y_Mars_list,'rs:')
Out[35]:
[<matplotlib.lines.Line2D at 0x1cb5a96a490>]
No description has been provided for this image

Labeling plots¶

When graphing data, we should (almost) always include:

  • A title, which can be added using the title function.
  • Axis labels, which can be added using the xlabel and ylabel functions.

When plotting several curves in a single figure, we should also label the curves and include a legend. When calling the plot function, we can use the label keyword argument to give a label to the data being plotted. We can then use the legend function to add a legend to the figure.

Note: Titles and labels can include LaTeX.

Exercise: Replicate the previous plot and add appropriate labels.

In [41]:
plt.plot(t_list, y_Earth_list,'b^--', label='Earth')
plt.plot(t_list, y_Mars_list,'rs:', label='Mars')

plt.title('Trajectories of objects in freefall')
plt.xlabel('Time (s)')
plt.ylabel('Height (m)')
plt.legend()
Out[41]:
<matplotlib.legend.Legend at 0x1cb5be6b9d0>
No description has been provided for this image

Further tweaks: axis limits, grid lines¶

We can use plt.grid() to add grid lines to the interior of the plot.

In [42]:
plt.plot(t_list, y_Earth_list,'b^--', label='Earth')
plt.plot(t_list, y_Mars_list,'rs:', label='Mars')

plt.title('Trajectories of objects in freefall')
plt.xlabel('Time (s)')
plt.ylabel('Height (m)')
plt.legend()

plt.grid()
No description has been provided for this image

We can change the horizontal and vertical limits using the plt.xlim and plt.ylim function.

Exercise: Modify the horizontal and vertical limits to approximate how long it takes for the object to hit the ground (i.e. reach $y(t)=0$) on Earth.

In [43]:
plt.plot(t_list, y_Earth_list,'b^--', label='Earth')
plt.plot(t_list, y_Mars_list,'rs:', label='Mars')

plt.title('Trajectories of objects in freefall')
plt.xlabel('Time (s)')
plt.ylabel('Height (m)')
plt.legend()

plt.xlim(14,15)
plt.ylim(-10,10)

plt.grid()
No description has been provided for this image

Suppose that we want to evaluate the function $y(t)$ on a more densely-packed list of $t$-values. Can we come up with a formulaic method for constructing such a list of $t$-values to be plugged in?

In [44]:
a = 0
b = 16
N = 1000 # The number of sub-intervals

dt = (b-a)/N

t_list = [ a + i*dt for i in range(N+1) ]
y_Earth_list = [ y(t,1000) for t in t_list ]
y_Mars_list = [ y(t,1000, g=-3.71) for t in t_list ]
In [46]:
plt.plot(t_list, y_Earth_list,'b--', label='Earth')
plt.plot(t_list, y_Mars_list,'r:', label='Mars')

plt.title('Trajectories of objects in freefall')
plt.xlabel('Time (s)')
plt.ylabel('Height (m)')
plt.legend()

plt.grid()
No description has been provided for this image