Plotting in Python can be achieved using the matplotlib library. There are a multitude of resources for learning how to use this library.
A full tutorial can be found at: Creating publication-quality figures with Matplotlib.
The first thing to do is to import a couple of libraries. In order to start plotting with matplotlib, you have to import the pyplot framework.
Additionally, we'll include a little tag to help it plot inside this notebook.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
/home/niall/.local/lib/python3.5/site-packages/matplotlib/backends/backend_gtk3agg.py:18: UserWarning: The Gtk3Agg backend is known to not work on Python 3.x with pycairo. Try installing cairocffi. "The Gtk3Agg backend is known to not work on Python 3.x with pycairo. "
Now, in order to plot something, we need some data! Let's generate our x-axis $[-\pi,\pi]$ by calling numpy's linspace function.
x0, x1 = 0, 2*np.pi
x = np.linspace(x0, x1, 1000)
The two functions we'll plot will be $\sin x$ and $\cos x$.
s = np.sin(x)
c = np.cos(x)
We'll showcase three examples:
To do this, we need to create a Figure using matplotlib's figure module.
fig1 = plt.figure();
Let's label the axes and toggle the limits of the graph.
In order to use $\LaTeX$ with your plot labels, start your strings with:
r'
then type in the text you want:
r'Axis Title/
when you want to use some TeX code, then start the inline mode with "$"
r'Axis Title/$\mu$
and close the string with a '.
r'Axis Title/$\mu$'
# Label the axes
plt.xlabel(r'$x$')
plt.ylabel(r'$\sin x$')
# Setting the x and y limits
plt.xlim([x0, x1])
plt.ylim([-1, 1])
(-1, 1)
Finally, let's plot the graph.
plt.plot(x, s);
Start by defining a new figure and setting the labels and limits of the axes.
fig2 = plt.figure()
# Label the axes
plt.xlabel(r'$x$')
# Setting the x and y limits
plt.xlim([x0, x1])
plt.ylim([-1, 1])
(-1, 1)
It is quite simple to plot two sets of data on a figure. One simply needs to call python plt.plot
twice.
We can specify some additional arguments to label the axis and change the colours.
# Plot the sin in red
plt.plot(x, s, label=r'$\sin x$', color='r')
# Plot the cos in green
plt.plot(x, c, label=r'$\cos x$', color='g')
[<matplotlib.lines.Line2D at 0x7ff2351a1518>]
We can then add a legend to our plot.
plt.legend()
<matplotlib.legend.Legend at 0x7ff2351a1048>
This time we need to do things a little differently. We need to call python plt.subplots()
.
There are two functions so we can plot two subplots, python ax
and python ax1
.
The number $121$ signifies the following:
fig3 = plt.figure()
ax = plt.subplot(121);
ax1 = plt.subplot(122);
Now, we plot the $\sin x$ to the plot on the left (ax
) and $\cos x$ on the plot on the right (ax1
).
Notice that in order to change the xlimits you have to call python ax.set_xlim()
instead of plt.xlimit()
.
The subplot labels overlap with the plots, we can fix this by calling python plt.tight_layout()
.
plt.tight_layout()
This task will get you to use the plotting libraries to plot some of the Hydrogen wavefunctions.
The wavefunctions you'll need for this task are as follows:
\begin{equation} \psi_{100} = \frac{1}{\pi}\bigg(\frac{Z}{a_0}\bigg)^\frac{3}{2} \ e^{-\frac{Zr}{a_0}} \end{equation}\begin{equation} \psi_{200} = \frac{1}{4\sqrt{2\pi}}\bigg(\frac{Z}{a_0}\bigg)^\frac{3}{2} \bigg(2 - \frac{Zr}{a_0} \bigg) \ e^{-\frac{Zr}{2a_0}} \end{equation}\begin{equation} \psi_{210} = \frac{1}{4\sqrt{2\pi}}\bigg(\frac{Z}{a_0}\bigg)^\frac{3}{2} \frac{Zr}{a_0} \ e^{-\frac{Zr}{2a_0}} \ \cos \theta \end{equation}Plot the Radial Probability distributions of the given wavefunctions.
\begin{equation} Radial \ Probability = 4\pi r^2 \times | R_{n\ell} | ^2 \end{equation}Where $R_{n\ell}$ is the radial component of the given wavefunctions.
It may be helpful to plot the functions against the scaled radius:
\begin{equation} \rho = \frac{Zr}{a_0} \end{equation}# Define your functions to calculate the radial probabilities here
import numpy as np
Z = 1
bohr_radius = 1.0
C = (Z / bohr_radius)
def radial_probability_100(x, y, z):
r = np.sqrt(x ** 2 + y ** 2 + z ** 2)
return (4 * np.pi * r ** 2 ) * ( (1/np.pi) * C ** (3/2) * np.exp(-C*r) ) ** 2
def radial_probability_200(x, y, z):
r = np.sqrt(x ** 2 + y ** 2 + z ** 2)
return (4 * np.pi * r ** 2 ) * ( (32 * np.pi) ** (-1/2) * C ** (3/2) * ( 2 - C*r ) * np.exp(- 0.5 * C * r) ) ** 2
def radial_probability_210(x, y, z):
r = np.sqrt(x ** 2 + y ** 2 + z ** 2)
cosine = z / r
return (4 * np.pi * r ** 2 ) * ( (32 * np.pi) ** (-1/2) * C ** (3/2) * C * r * np.exp(-0.5 * C * r) * cosine) ** 2
Now, plot the radial probabilities on a single figure. Label your axes and each distribution accordingly.
# Plot your results using the functions you have defined above.
space_range = 10 * bohr_radius
x, y, z = np.linspace(-space_range, space_range, 1000), np.linspace(-space_range, space_range, 1000), np.linspace(-space_range, space_range, 1000)
r = np.sqrt(x ** 2 + y ** 2 + z ** 2)
plot = plt.figure()
plt.plot(r, radial_probability_100(x, y, z), label=r'$RDF_{100} $')
plt.plot(r, radial_probability_200(x, y, z), label=r'$RDF_{200} $')
plt.plot(r, radial_probability_210(x, y, z), label=r'$RDF_{210} $')
plt.xlabel(r'$r/a_0$')
plt.legend()
<matplotlib.legend.Legend at 0x7ff23fe2ecf8>
# Plot in the X-Z plane
X, Z = np.meshgrid(x, z)
# Create our new figure
contour_fig = plt.figure()
# Create the wavefunction data
rd100 = radial_probability_100(X, 0, Z)
rd200 = radial_probability_200(X, 0, Z)
rd210 = radial_probability_210(X, 0, Z)
# Tidy up on sub-plots
ax = plt.subplot(221);
ax1 = plt.subplot(222);
ax2 = plt.subplot(223);
# Set the axis
ax.set_xlim([-4*C,4*C])
ax.set_ylim([-4*C,4*C])
ax1.set_xlim([-15*C,15*C])
ax1.set_ylim([-15*C,15*C])
# Et, la pièce de résistance:
ax.contour(X, Z, rd100)
ax1.contour(X, Z, rd200)
ax2.contour(X, Z, rd210)
<matplotlib.contour.QuadContourSet at 0x7ff2246f4f60>