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()
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.
figure function defines the start of a new figure
By default the plot will extend to the beginning and end of the plot.
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
# 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
plotneed to be the same size.
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()
Adding axis labels
To add a label to an axis you can use the
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()
Adding a grid
To add a grid in
matplotlib you simply need to use the
grid function. This function takes two optional arguments,
minor, which refer to the major and minor gridlines respectively.
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()
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
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'), and to change the scale to a logarithmic scale you would use
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()
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
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()
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()