Python Friday #169: Style Your Plots in Matplotlib

The default style of Matplotlib is not that visually pleasing. Before we switch to a different library, we explore the various styles that Matplotlib offers.

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.

 

List the available styles

You can get a list of all the styles installed for Matplotlib with this command:

Solarize_Light2
_classic_test_patch
_mpl-gallery
_mpl-gallery-nogrid
bmh
classic
dark_background
fast
fivethirtyeight
ggplot
grayscale
seaborn-v0_8
seaborn-v0_8-bright
seaborn-v0_8-colorblind
seaborn-v0_8-dark
seaborn-v0_8-dark-palette
seaborn-v0_8-darkgrid
seaborn-v0_8-deep
seaborn-v0_8-muted
seaborn-v0_8-notebook
seaborn-v0_8-paper
seaborn-v0_8-pastel
seaborn-v0_8-poster
seaborn-v0_8-talk
seaborn-v0_8-ticks
seaborn-v0_8-white
seaborn-v0_8-whitegrid
tableau-colorblind10

 

Create an example for each style

You find an example for each style in the documentation. However, if you are like me, you like to see the style in action with your own data. We can do that with this code:

This creates a figure for each of the 28 styles with our basic bar plot. I selected these 3 styles to show you how different they are:

The bar plot with a yellowish background from Solarize_Light2
The bar plot with dark background from dark_background
The bar plot with a grey grid as background from seaborn-v0_8-darkgrid

 

Fix the too many figures runtime warning

If you run the code from above to create a demo figure for each style, you may run into that problem:

RuntimeWarning: More than 18 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam figure.max_open_warning). Consider using matplotlib.pyplot.close().

Is this the case, increase the value for the “matplotlib.pyplot.figure” property.

However, even if you increase the value, your notebook may stop showing you the figures. In this case you can add this special command as the first line in your plot cell:

This tells the notebook once more that you want to have the inline figures and for me that fixed the most annoying problems.

 

Change the style of a single figure

If we want to change the style of a single figure, we can wrap our plot in a with block like this:

Only this figure will now use the bmh style:
Our figure shows the grid and has a light grey background

 

Change the style of all figures

If we want to change all plots in our notebook, we can use this line right after the Matplotlib imports:

This changes the default style and works for all plots until we change the style again.

 

Next

The built-in styles offer us an improvement over the default style. If that is not enough, we can go and install additional styles. Next week we explore a simple way to make sure that everyone understands that our plots are a draft and not ready for production.

1 thought on “Python Friday #169: Style Your Plots in Matplotlib”

Leave a Comment

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