Plotting with Matplotlib’s Procedural Interface
Plotting with Matplotlib’s Procedural Interface¶
The first type of plot we will create using matplotlib are line and scatter plots. These are plots where we specify
y values, or
We will use the data file we discussed in the previous section to create the plots,
s_orbitals_1D.csv. This data file contains the value of the
3s orbital as a function of distance from the center of the atom (
We’ll start by importing the libraries we will need -
pandas for reading the data file, and
matplotlib for creating the plot. We will read in our data using the
pd.read_csv function that we used in the previous lesson.
The first plots we create will use
matplotlib’s procedural interface. This interface is designed to mimic MATLAB’s plotting procedure.
import pandas as pd import matplotlib.pyplot as plt s_orbitals = pd.read_csv("s_orbitals_1D.csv")
To make our plots interactive in the notebook, we use something called a jupyter notebook “magic” command. These commands start with a percent sign (
%). These are not python commands, they are special for the jupyter notebook. The following will make our plots interactive. There is no output from this command.
When using the procedural interface, you always use commands which start with
plt.function_name. To create a line plot, we use first create a figure using
plt.figure, then we add data to the figure using
plt.figure() plt.plot(s_orbitals["r"], s_orbitals["1s"])
[<matplotlib.lines.Line2D at 0x7fbd43636dc0>]
The line will be plotted on the most recently created figure.
If we want to add more data to oour figure, we use additional
plt.figure() plt.plot(s_orbitals["r"], s_orbitals["1s"]) plt.plot(s_orbitals["r"], s_orbitals["2s"]) plt.plot(s_orbitals["r"], s_orbitals["3s"])
[<matplotlib.lines.Line2D at 0x7fbd44c0a9d0>]