Introduction to Matplotlib

Now that we can start doing serious numerical analysis with Numpy arrays, we also reach the stage where we can no longer print out hundreds or thousands of values, so we need to be able to make plots to show the results.

The Matplotlib package can be used to make scientific-grade plots. You can import it with:

In [ ]:
import matplotlib.pyplot as plt

In a jupyter notebook, it is often helpful to add a cell containing:

In [ ]:
%matplotlib inline

(%matplotlib is a so-called "magic function") so that the plots will appear inside the notebook.

In more recent versions of matplotlib there is also %matplotlib notebook which is an interactive output.

Basic plotting

The main plotting function is called plot:

In [ ]:
plt.plot([1,2,3,6,4,2,3,4])

In the above example, we only gave a single list, so it will assume the x values are the indices of the list/array.

However, we can instead specify the x values:

In [ ]:
plt.plot([3.3, 4.4, 4.5, 6.5], [3., 5., 6., 7.])

Matplotlib can take Numpy arrays, so we can do for example:

In [ ]:
import numpy as np
x = np.linspace(0., 10., 50)
y = np.sin(x)
plt.plot(x, y)

The plt.plot() function is actually quite complex, and for example can take arguments specifying the type of point, the color of the line, and the width of the line:

In [ ]:
plt.plot(x, y, marker='o', color='green', linewidth=2)

The line can be hidden with:

In [ ]:
plt.plot(x, y, marker='o', color='green', linewidth=0)

If you are interested, you can specify some of these attributes with a special syntax, which you can read up more about in the Matplotlib documentation:

In [ ]:
plt.plot(x, y, 'go')  # means green and circles

Exercise 1

We start off by loading the data/munich_temperatures_average_with_bad_data.txt file which we encountered in the introduction to Numpy (section 10):

In [ ]:
# The following code reads in the file and removes bad values
import numpy as np
date, temperature = np.loadtxt('data/munich_temperatures_average_with_bad_data.txt', unpack=True)
keep = np.abs(temperature) < 90
date = date[keep]
temperature = temperature[keep]

Now that the data has been read in, plot the temperature against time:

In [ ]:
# your solution here

Next, plot the data against the fraction of the year (all years on top of each other). For this, the % (modulo) operator is useful to find the fractional part of the dates: 1982.2762%1 = 0.2762

In [ ]:
# your solution here

Other types of plots

Scatter plots

While the plt.plot() function can be used to show scatter plots, it is mainly used for line plots, and the plt.scatter() function is more often used for scatter plots, because it allows more fine control of the markers:

In [ ]:
x = np.random.random(100)
y = np.random.random(100)
plt.scatter(x, y)

Histograms

Histograms are easy to plot using the plt.hist() function:

In [ ]:
v = np.random.uniform(0., 10., 100)
h = plt.hist(v,bins=[0,2,4,6,8,10],density=True)  # we do h= to capture the output of the function, but we don't use it (here)

In older version of python, the option to normalize the histogram was called normed=True, but this now often puts out warnings or does not work anymore.

Images

You can also show two-dimensional arrays with the plt.imshow() function:

In [ ]:
array = np.random.random((64, 64))
plt.imshow(array)

And the colormap can be changed:

In [ ]:
plt.imshow(array, cmap=plt.cm.gist_heat)

Contours

Often, countour plots are useful to illustrate 3D data. This can be done using plt.contour():

In [ ]:
x = np.arange(-2.0, 2.0, 0.005)
y = np.arange(-2.0, 2.0, 0.005)
xx, yy = np.meshgrid(x, y)
zz = np.exp(-xx**2 - yy**2)*(xx-yy)**2

plt.axes().set_aspect('equal')        # equal aspect ratio

contour_set = plt.contour(xx, yy, zz) # returns a set of contour lines for plt.clabel
plt.clabel(contour_set)               # creates labels with gaps in the contours

In the above, we used the np.meshgrid() function which is useful when the plotting functions (and also other functions in general) require the X and Y coordinates in a special format. To understand the np.meshgrid() function better, consider the following example:

In [ ]:
# For example:
x = [1,2,3]
y = [1,2]
a, b = np.meshgrid(x, y)
In [ ]:
print(np.shape(a), np.shape(b))
print(a)
print(b)

So for each of the chosen x and y points, a and b contain the values of x and y, respectively.

In [ ]:
# at coordinate (0|1), we have (note: the first index determines y, the second x)
print(x[0], y[1], a[1][0], b[1][0])

Field lines

For a vector field, you can plot a depiction of the field lines with plt.streamplot()

Here this is done in the x-y plane for the field $$\vec{K}(\vec{r})=\vec{K}(x,y,z) = (-y,x,0) = \vec{e}_z \times \vec{r}$$

We again use np.meshgrid() :

In [ ]:
x=np.arange(-10,10,0.1)
y=np.arange(-10,10,0.1)

xx,yy=np.meshgrid(x,y)  # define meshgrid of all coordinates
                        # (can also work out radii etc)
kx =-yy
ky = xx

plt.streamplot(x,y,kx,ky,color='k')

Customizing plots

You can also customize plots. For example, the following code adds axis labels, and sets the x and y ranges explicitly:

In [ ]:
x = np.random.random(100)
y = np.random.random(100)
plt.scatter(x, y)
plt.xlabel('x values')
plt.ylabel('y values')
plt.xlim(0., 1.)
plt.ylim(0., 1.)

Saving plots to files

To save a plot to a file, you can do for example:

In [ ]:
x = np.random.random(100)
y = np.random.random(100)
plt.scatter(x, y)
plt.xlabel('x values')
plt.ylabel('y values')
plt.xlim(0., 1.)
plt.ylim(0., 1.)

plt.savefig('my_plot.png')

and you can then view the resulting file like you would view a normal image (check the current folder).

Interactive plotting

One of the features of Matplotlib is the ability to make interactive plots. When using IPython, you can do:

%matplotlib qt

or

%matplotlib notebook

(somwhat dependent on the version) to change the backend to be interactive, after which plots that you make will be interactive.

Learning more

The easiest way to find out more about a function and available options is to use the ? help in IPython:

    In [11]: plt.hist?

Definition: plt.hist(x, bins=10, range=None, normed=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, hold=None, **kwargs)
Docstring:
Plot a histogram.

Call signature::

  hist(x, bins=10, range=None, normed=False, weights=None,
         cumulative=False, bottom=None, histtype='bar', align='mid',
         orientation='vertical', rwidth=None, log=False,
         color=None, label=None, stacked=False,
         **kwargs)

Compute and draw the histogram of *x*. The return value is a
tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*,
[*patches0*, *patches1*,...]) if the input contains multiple
data.

etc.

But sometimes you don't even know how to make a specific type of plot, in which case you can look at the Matplotlib Gallery for example plots and scripts.

Exercise 2

  1. Use Numpy to generate 10000 random values following a Gaussian/Normal distribution using np.random.normal(), and make a histogram. Adjust the number of bins to properly see the Gaussian. Over-plot a Gaussian function using a colored line, and adjust the normalization so that the histogram and the line are aligned.

  2. Do the same for a Poisson distribution. Compare the Poisson distribution for expectation values $\lambda <15$ with the appropriate Gaussian.

In [ ]:
# your solution here

Exercise 3 [Bonus]

Work out the magnetic field lines of two equal but infinite line currents along the z-axis that are separated by a distance $a$ along the y-axis.

Since the stream lines are scaled, one can drop constants.

The $\vec{B}$-Field of a single line current $I$ is $$\vec{B} (\vec{r})= \frac{\mu_0 I}{2 \pi} \frac{1}{|\vec{r}|} \vec{e}_{\varphi}$$

Only with time left: Can you plot the fieldlines in the x-y plane for a circular coil with radius a in the x-z plane? The Biot-Savart law for a wire element is

$$d\vec{B} (\vec{r})= \frac{\mu_0 I}{4 \pi} \frac{d \vec{l} \times \vec{r}}{|\vec{r}|^3}$$

where $\vec{r}$ is the vector from the current element to the place of measurement.

You can even code the problem in a way that you can add an aribitrary number of coils, e.g. for a long coil or a ring coil (torus).

In [ ]:
# your solution here