In addition to the very efficient numerical computation that is possible with the NumPy array, another majoring selling point of the Python language for scientific applications is the ability to produce high-quality plots and graphs easily.
This is due to the package known as
matplotlib, pronounced mat-plot-lib.
matplotlib is particularly useful in combination with the NumPy arrays introduced previously.
Let’s see an example,
import numpy as np import matplotlib.pyplot as plt x = np.linspace(-5, 5, 9) print("Values in the array:", x) plt.plot(x, x ** 2) plt.show()
Values in the array: [-5. -3.75 -2.5 -1.25 0. 1.25 2.5 3.75 5. ]
In the cell above we do the following:
numpypackage and store it with the shortcut
pyplotlibrary from the
matplotlibpackage, with the shortcut
pltis the standard shortcut).
Generate an array containing nine values, linearly-spaced from -5 to 5. These values are then printed.
Draw a plot with the array
xon the x-axis and the square of this array on the y-axis.
Show the plot. This final function returns
print()) but shows the plot in the notebook.
The popularity of
matplotlib is driven heavily by the broad capability of the package.
We will look quickly at some customisation that is possible:
Adding axis titles.
Changing the plot type (line, scatter, etc.).
Using logarithmic axes.
Plotting data with error bars. As in much of the Python programming language, if you want to do something with
matplotlibthere is probably a stackoverflow question about it, so feel free to play about and learn other functionality.
Let’s consider plotting the concentration of 19F as it decays to 18O, following a first-order rate law, the data for which is in the table below.
We will store each of these datasets (the time, the concentration, and the uncertainty in the concentration) as individual NumPy arrays.
time = np.array([0, 60, 360, 600, 3600, 14400]) conc = np.array([9.97, 9.91, 9.60, 9.36, 6.83, 2.19]) u_conc = np.array([0.50, 0.50, 0.48, 0.47, 0.34, 0.11])
We can now plot the x and y data without error bars, using a similar command to the one above.
plt.plot(time, conc, 'o') plt.xlabel('Time/s') plt.ylabel('Concentration/M') plt.show()
Note, that the
'o' in the
plt.plot() function resulted in a scatter plot, with no line joining the points.
Additionally, strings given to the
plt.ylabel() commands are added as axis labels.
We can also produce a
matplotlib plot that shows the uncertainty measurements as error bars and scales the axis to make things a bit clearer.
plt.errorbar(time, conc, u_conc, marker='o') plt.xlabel('Time/s') plt.ylabel('Concentration/M') plt.yscale('log') plt.show()
plt.errorbar() function is used instead of the
plt.plot() function, be aware that the
'o' argument will only work for this function if preceded by
Additionally, we can produce a logarithmic y-axis (particularly useful for this example with a first-order rate law) using the
plt.yscale() command (a similar
plt.xscale() command also exists.