Skip main navigation

Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. T&Cs apply

Matplotlib

A brief description of Matplotlib, with some simple examples

This article provides a quick overview of the Python plotting tool Matplotlib.

An important part of any work involving the use of quantitative data is visualisation of that data and the results of your analyses. If your data analysis takes place in Python, it makes sense to produce your plots and data visualisation within Python too. The most common Python package for doing this is Matplotlib (https://matplotlib.org/). This comes pre-installed with Anaconda but otherwise you can install it easily using pip:

 pip install matplotlib

Some examples

Below are some simple examples to give you a feel of how Matplotlib works. A complete version of the code to produce all the examples is available from our Github: https://github.com/LAR/PhenoDataCampp/tree/main/MachineLearning

Line plots

To make a plot with Matplotlib all you need is a set of x values and a set of y values. This can be a Python list, or as in the example below, a Numpy array. Once you have the values you want to plot, you make the plot with plt.plot(x,y) and display it with plt.show():

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5, 5, 1000)
y = np.cos(x)

plt.plot(x, y)
plt.show()
plt.close()

This code should make and display a figure in a popup window like the one below. For your script to continue or terminate you will need to close the window in the usual way for any window based system, with the cross.

A simple line plot of the cosine function using Matplotlib

As you can see the default behaviour of plot is to make a line plot.

To save a figure, rather than displaying it with show(), you need to use the savefig() function as follows:

plt.savefig('my_figure_name.png')

Note: it’s important to use plt.savefig before or instead of plt.show, rather than afterwards. Otherwise, if you use plt.show first, the plot is displayed first in a new window, then when you close it is cleared from memory. So plt.savefig just saves an empty plot.

If you want to plot more than one line on your axes you can just add more plot commands as follows:

x = np.linspace(-5, 5, 1000)
y1 = np.cos(x)
y2 = np.sin(x)

plt.plot(x, y1,'r')
plt.plot(x, y2,'g')

The strings ‘r’ and ‘g’ after the x,y part specify the colours of the lines to be red and green respectively.

Scatter plots

If your data are separate observations, it’s common to want to plot them as discrete points in a scatter plot. To do this in Matplotlib you can just add a marker character such as ‘X’ to the plot command, possibly along with a colour code as we’ve done for the first plot below. Alternatively, you can be more explicit and use the scatter function along with the keyword arguments marker and color. We’ve included both ways in the code below using some randomly simulated data:

x = np.linspace(0,10,15)
y1 = np.random.random(15)
y2 = np.random.random(15)

plt.plot(x,y1,'Xr') # scatter plot chosen by adding 'X' character in string
plt.scatter(x,y2,marker='o',color='b') # scatter plot chosen explicitly

plt.show()

Randomly generated scatter plot data, with two separate series plotted as circles and crosses

For a full list of colour and marker codes you can use see https://matplotlib.org/cheatsheets/.

Of course, all good plots need to be well annotated and laid out, and there are functions in Matplotlib to do all this. Try following though the code below using the comments, which displays the same random data series as above, but with additional axes labels, axes limits, a figure legend, and a title:

x = np.linspace(0,10,15)
y1 = np.random.random(15)
y2 = np.random.random(15)


# it's often useful to explicitly make blank figures and axes
# so you can then refer to them directly

fig = plt.figure() # make a blank figure
ax = plt.axes() # add some axes


ax.plot(x,y1,'Xr',label='apples') # notice the keyword label
ax.scatter(x,y2,marker='o',color='b', label='oranges')

ax.legend() # add legend (using the labels set above)

ax.set_xlim([-5,15]) # set limits of x-axis
ax.set_ylim([-0.5,1.5]) # set limits of y-axis

ax.set_xlabel('some x values') # set label of x-axis
ax.set_ylabel('some y values') # set label of y-axis



ax.set_title('random fruit data')

plt.show()

The same data as the previous figure, but with annotated axes and legend

There’s much more to Matplotlib then we can cover here, so we would encourage you to look at the examples and documentation on the Matplotlib website if you want to learn more. Other plot types include:

  • histograms
  • bar charts
  • error bars
  • contour plots
  • range fills.
This article is from the free online

Machine Learning for Image Data

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now