Python Friday #168: Customise Your Plots in Matplotlib

Matplotlib gives its best to create a plot for us. But sometimes that result can be cleaned-up and improved to better represent the interesting parts of our data. Let us find out how we can do that.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

What can we customise?

The section “Parts of a Figure” in the Matplotlib tutorials has this nice graphic that shows what we can customise:

Nearly every aspect of a figure and the axes can be customised.

If we look at the different parts, we can safely say that Matplotlib allows us to customise nearly every aspect of a plot. Since we find all those details in the documentation, I only cover the most often customised parts of a figure. For everything else, check the documentation.

I strongly suggest you keep the official cheat sheet and the handouts for Matplotlib near you. You can find them on matplotlib.org/cheatsheets.

 

Name your axes

Nothing is more useless than a plot where we have no idea about the axes. Is it apples? Bananas? Number of cars? We can use the set_xlabel() and set_ylabel() methods to set the labels for our axes:

IMAGE:
The axes show now what the plot is all about

 

Set a title

Especially when we have multiple axes in a figure, we should set a title to keep them apart. We can do that with the set_title() method:

The axes has now a title

 

Tweak the axis ticks

We can format and configure the major and minor ticks on an axis. To show only whole numbers, we can use the MultipleLocator and set it to a multiple of 1:

This gives us a plot with only whole numbers in the X axis:

The .5 values are no longer visible

We can change the orientation of the minor ticks while keeping the major ticks unchanged:

Especially when we do not have enough space is a change of the orientation a useful customisation:

The minor ticks are now vertical, while the major ticks of the X axis are horizontal

 

Change the size of your figure

We can change a single figure with the figsize() parameter, that takes the width and the hight in inches:

This gives us a 2 by 2 inches plot:

The plot is now much smaller than usual

If we want to change the size of all plots in a notebook, we can set the figsize in plt.rcParams() right after we import Matplotlib:

 

Change the line of your line plot

In a line plot, we can change the thickness of the line, the line style, the colour and the marker for our points:

This gives us one plot with a thicker line, one with a red line, one with a dashed line and one with visible dots for the points:

4 line plots with different attributes changed (thicker line, red line, dashed line, visible dots)

 

Change the bins or the range in your histogram

Matplotlib makes a guess on a useful number of bins for a histogram (a bin is a range of values that are represented by a bar). While the default is often a good fit, it is not always the case. We can change the number of bins or show only a range of data to improve our plot:

This creates four axes for us. The first one uses the default settings, in axes 2 and 3 we change the number of bins and in the 4th we select only a part of the data:

4 different histograms created from the same data.

 

Next

The tricks presented in this post allow us to display the data in a way that fits our needs. Next week we will explore the different styling options Matplotlib offers for our figures.

2 thoughts on “Python Friday #168: Customise Your Plots in Matplotlib”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.