Xtralien Scientific Python: Plotting with matplotlib

 

In Python you typically plot data using the matplotlib library. This tutorial shows you how to make a simple plot and customise it for presentation.

Your first plot

matplotlib is a library that allows for easy plotting of data in Python.

x = [1, 2, 3, 4, 5]
y = [3, 5, 7, 8, 9]

figure()
plot(x, y)
show()

Note: figure() and show() are optional in the Xtralien Scientific Python, however they are recommended as they are explicit and can show you where your figure starts and ends.

The above example shows the most common plot, the line plot. the plot function takes a list of x values, and a list of y values. These two lists should be equal in length as they will be matched to each other when plotting, e.g. (1, 3), (2, 5).

The figure function defines the start of a new figure

Simple plot, generated by the above code.

By default the plot will extend to the beginning and end of the plot.

Multiple plots

Sometimes it is useful to plot multiple lines on a graph to compare series of data. Because matplotlib stores previous graphs until you clear the figure (using clf), it is possible to plot multiple graphs on the same figure by repeating the plot command.

# x values
x = [1, 2, 3, 4, 5]
# First y series
y1 = [1, 2, 3, 4, 5]
# Second y series
y2 = [1, 4, 9, 16, 25]

# Plot everything
figure()
plot(x, y1)
plot(x, y2)
show()

The code above will produce a plot with two lines for each of the different plots. These are different colours to easily differentiate the two.

Note: The x does not need to be shared as the graph will expand to fit the largest plot. However the x and y lists for each indiviual plot need to be the same size.

Double plot, generated by the above code.

Customising your plot

It is possible to further customise your plot to make it easier to interpret, or to stylise your results.

Adding a title

Adding a title is useful when creating figures that you plan to publish, or simply to explain at a glance what the plot shows. The function that matplotlib contains to do this is title. To use title you just pass the string you wish to use as the title.

x = [1, 2, 3, 4, 5]
y = [3, 5, 7, 8, 9]

figure()
plot(x, y)
title("2x + 1")
show()
Plot with a single line that has a title of 2x + 1.

Adding axis labels

To add a label to an axis you can use the xlabel and ylabel functions in matplotlib. These will add a labels to each relevant axis. Both of these functions take a string that will be used as the relevant axis label.

x = [1, 2, 3, 4, 5]
y = [3, 5, 7, 9, 11]

figure()
plot(x, y)
xlabel('x')
ylabel('y')
show()
A plot with axis labels shown on both axes.

Adding a grid

To add a grid in matplotlib you simply need to use the grid function. This function takes two optional arguments, major and minor, which refer to the major and minor gridlines respectively.

By default major is True and minor is False, resulting in a grid that shows the major gridlines.

x = [1, 2, 3, 4, 5]
y = [3, 5, 7, 9, 11]

figure()
plot(x, y)
grid(major=True, minor=False)
show()
A plot generated from the above code, with a grid enabled, showing only the major gridlines.

Changing Scale

When plotting using matplotlib most plots will either use a linear scale, or logarithmic scale. Depending on the effect that you are investigating both may be of interest.

The easiest method of changing the scale is through the use of the xscale and yscale functions. Both work similarily and take a string as an argument.

To change the y axis scale to linear mode you would use yscale('linear'). Whereas, to change the scale to a logarithmic scale you would use yscale('log').

x = [1, 2, 3, 4, 5]
y = [1e1, 1e2, 1e3, 1e4, 1e5]

# Linear plot
figure()
plot(x, y)
yscale('linear')
show()

# 
figure()
plot(x, y)
yscale('log')
show()
The first plot from the above code, showing a linear y scale.
The second plot from the above code, showing a logarithmic y scale.

Changing axis limits

Axis limits are customisable for each plot, and can be changed by providing the minimum and maximum values for each of the limits respectively.

You can use the xlim and ylim functions for this purpose. An example of when you would want to do this is if you wish to focus on a specific point, such as a point of intersection between two lines.

# x values
x = [1, 2, 3, 4, 5]
# First y series
y1 = [1, 2, 3, 4, 5]
# Second y series
y2 = [1, 4, 9, 16, 25]

# Plot everything
figure()
plot(x, y1)
plot(x, y2)
# Set the limits
xlim(2, 3)
ylim(5, 6)
# Show the plot
show()
The plot generated by the above code, showing a clipped plot, with limits on the x-axis of 2,3 and limits on the y-axis of 5,6.

Creating a legend

It is possible to create a legend in matplotlib by using the legend function. This function creates a legend in a corner of the plot.

To label this legend you need to supply a label to every plot that you create. label is a string, and will appear next to the line in the legend.

# x values
x = [1, 2, 3, 4, 5]
# First y series
y1 = [1, 2, 3, 4, 5]
# Second y series
y2 = [1, 4, 9, 16, 25]

# Plot everything
figure()
plot(x, y1, label="2x + 1")
plot(x, y2, label="x*x")
show()
A plot showing two lines, with a legend illustrating them.