avatarMatt Crooks

Summary

This is a guide on how to set up rcParams, a dictionary of most Matplotlib styling properties, to customize and improve the appearance of default plotting parameters in matplotlib.

Abstract

In this article, the author discusses the benefits and usage of Matplotlib, a popular Python data visualization library, in comparison to other alternatives. The focus is on its ability to set default plot appearance using rcParams. The author provides a walkthrough of how to manipulate various properties such as axes, ticks, text, figure sizes, and plot styles by modifying the rcParams dictionary. The article is intended to help users improve the aesthetics and consistency of their Matplotlib plots.

Opinions

  • The author believes that Matplotlib is unfairly criticized and is the best Python plotting package available due to its customization and default appearance capabilities.
  • Seaborn, a popular alternative, while having additional plot types, is considered less favorable due to its reliance on Matplotlib's underlying structure for modifications.
  • Plotly, another alternative, receives criticism for its limited customization options and documentation for default appearance settings.
  • The author emphasizes the importance of setting defaults at the start of the notebook and applying them consistently across all Matplotlib figures.
  • They also mention that some changes made in rcParams still require addressing specific plot properties, such as title positions and line widths being cut off.
  • The author provides their recommended full set of rcParams for achieving a consistent, distinctive plot style.

How to Set Up rcParams in Matplotlib

A simple guide to setting default plotting parameters in matplotlib’s rcParam

Matplotlib gets a lot of stick I think it’s unfair. I’ll admit that if you just run plt.plot() then you’ll get something quite ugly but it is, in my view, the best plotting package in python and a largely unavoidable one.

Alternatives

Ok, so you don’t like matplotlib and are considering something else. Here’s my (brief) take on the popular alternatives.

Seaborn is a popular alternative plotting package that has loads of great additional plot types and will produce slightly better plots out the box. pandas can be used to plot data directly and can handle time series quite nicely, so again is popular among data scientists. However, both seaborn and pandas are matplotlib under the hood and if you want to change their default appearance then you need to know matplotlib. Please don’t get me started on Plotly…

The single greatest thing about matplotlib is the ability to set default plot appearance at the start of a notebook and it apply to all your matplotlib figures — some of these properties also apply to seaborn and pandas plots! There is nothing even remotely similar in Plotly in which every plot must be created from scratch. Although to be fair plotly does look better from the start it’s very cumbersome to update the defaults and the documentation is limited.

In my previous article I talked about how to change the appearance of matplotlib figures. In this article I’ll show you how to set these settings on all your plots using rcParams.

What is rcParams in matplotlib?

It’s a dictionary of most matplotlib styling that you set at the start of your notebook and it will apply to all your plots.

You can import it using the following:

from matplotlib import rcParams

Printing out rcParams in a notebook you can see the key-value pair structure.

The names are usually pretty helpful and matplotlib has great documentation to help you find out exactly what each option does. To change a value you simply treat it like any other dictionary:

Here the value of axes.grid has been updated from False to True.

Basic Matplotlib Plot

We’re going to create a simple plot in matplotlib and then I’ll take you through the most useful rcParams values to update and show the effect of each set.

Below is a code snippet that we’ll use to generate all our plots.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 1)
f, ax = plt.subplots()
ax.plot(x, x ** 2)
ax.plot(x, np.sin(2 * x))
ax.plot(x, 0.2 + 0.6 * x)
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')

The output is in the figure below

How to format axes in rcParams

How to turn spines on and off as well as setting axis color and axis width in rcParams

Let’s start with the axes. As I discussed in my previous article, I don’t like the box that is drawn around the plot so let’s turn them off in the rcParams.

rcParams[‘axes.spines.bottom’] = True
rcParams[‘axes.spines.left’] = False
rcParams[‘axes.spines.right’] = False
rcParams[‘axes.spines.top’] = False
rcParams['axes.linewidth'] = 2
rcParams['axes.edgecolor'] = 'grey'

I like to have the x axis remain but use grid lines instead of the y axis. Above I have removed everything except the x axis, which I also made more prominant by making it thicker but changed the colour to grey. To add the grid lines we need to first turn grid lines on and then tell matplotlib to make them only in the y direction.

rcParams['axes.grid'] = True
rcParams['axes.grid.axis'] = 'y'

You can set the thickness and colour using the following:

rcParams['grid.color'] = 'grey'
rcParams['grid.linewidth'] = 0.5

As a standard I like the x axis to lie across y = 0 so let’s remove the padding on the y axis limits

rcParams[‘axes.ymargin’] = 0

Finally, let’s just make sure the axes are always sat behind the actual plot. I can’t actually remember why I added this but I definitely had an issue with it at one point!

rcParams[‘axes.axisbelow’] = True

Let’s see what our plots now looks like

How to set Ticks in rcParams

The keen eye among you might notice that the ticks on the y axis are still there next to the grid lines and that they’re a bit thicker. I like to remove these because I don’t think they’re necessary. You can do this by setting the width and length to both be 0. Slightly confusingly, if you want to change the y tick labels then you still need to set the colour of the ticks to grey.

rcParams['ytick.major.width'] = 0
rcParams['ytick.major.size'] = 0
rcParams['ytick.color'] = 'grey'

You can then style the size, width, and colour of the x ticks

rcParams['xtick.major.width'] = 2
rcParams['xtick.major.size'] = 5
rcParams['xtick.color'] = 'grey'

Let’s see what our plots now looks like

How to set Text font, color and size in rcParams

You can set the size, font, and colour of text in rcParams as well. I find the text in matplotlib a little small as default — this is also true of seaborn and pandas.

rcParams['font.size'] = 16
rcParams['font.family'] = 'serif'
rcParams['text.color'] = 'grey'
rcParams['axes.labelcolor'] = 'grey'

Here I have also set the axis label colour to grey.

You might notice that now the x label has been cut off — we’ll sort that later.

How to set default Figure size in rcParams

I find these figures a bit small. For most applications, a single plot has a decent aspect ratio that works well on a laptop screen and in slide decks using this default:

rcParams['figure.figsize'] = (10, 7)

It also works better in a medium article!

Saving

When saving figures to add to slide decks I find the quality is a little low and also they appear with a white background which can sometimes obscure other things in you slides. I like to crop the image to remove excess white space, increase image quality, and set the background to transparent.

rcParams['savefig.bbox'] = 'tight'
rcParams['savefig.dpi'] = 500
rcParams['savefig.transparent'] = True

You may notice an increase crispness here already although you’ll have to take my word for it that the background is transparent!

Plot Styles

There are a lot you can do with the styling of the actual plots — i.e. the lines, markers, bars, etc. I’ll just cover the basics here with the lines above. I like to make the lines wider

rcParams['lines.linewidth'] = 3

You might also notice the tell tale signs of a matplotlib figure — the colours. You may also have wondered how matplotlib knows to plot the first thing blue, the second orange, and the third green. It’s something called a cycler and you can create these yourself using your own choice of colours! All you need to do is import cycler and then pass in a list of colours and matplotlib will cycle through these colours everytime you add a new plot element to your figure.

from cycler import cycler
navy = (56 / 256, 74 / 256, 143 / 256)
teal = (106 / 256, 197 / 256, 179 / 256)
pink = [199 / 255, 99 / 255, 150 / 255]
rcParams['axes.prop_cycle'] = cycler(color=[teal, navy, pink])

Limitations

There are a few limitations with rcParams that still need to be addressed when plotting:

  • When I add a title I like to shift it’s location up slightly and you can’t set that by default
  • You may notice in the above plots that the line width on the navy line has been cut of at the top

These can be addressed with the following

f, ax = plt.subplots()
ax.plot(x, x ** 2, clip_on=False)
ax.plot(x, np.sin(2 * x), clip_on=False)
ax.plot(x, 0.2 + 0.6 * x, clip_on=False)
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_title('My Title', y=1.03)

Conclusions

rcParams is a great feature of matplotlib that applies to both standard matplotlib plots as well as seaborn and pandas plots. In this article, I’ve covered my most commonly adjusted values that I use in all my notebooks to create a consistent and distinctive style of plot.

Full rcParams

Below is my full set of rcParams.

# -- Axes --
rcParams['axes.spines.bottom'] = True
rcParams['axes.spines.left'] = False
rcParams['axes.spines.right'] = False
rcParams['axes.spines.top'] = False
rcParams['axes.grid'] = True
rcParams['axes.grid.axis'] = 'y'
rcParams['grid.color'] = 'grey'
rcParams['grid.linewidth'] = 0.5
rcParams['axes.axisbelow'] = True
rcParams['axes.linewidth'] = 2
rcParams['axes.ymargin'] = 0
# -- Ticks and tick labels --
rcParams['axes.edgecolor'] = 'grey'
rcParams['xtick.color'] = 'grey'
rcParams['ytick.color'] = 'grey'
rcParams['xtick.major.width'] = 2
rcParams['ytick.major.width'] = 0
rcParams['xtick.major.size'] = 5
rcParams['ytick.major.size'] = 0
# -- Fonts --
rcParams['font.size'] = 16
rcParams['font.family'] = 'serif'
rcParams['text.color'] = 'grey'
rcParams['axes.labelcolor'] = 'grey'
# -- Figure size --
rcParams['figure.figsize'] = (10, 7)
# -- Saving Options --
rcParams['savefig.bbox'] = 'tight'
rcParams['savefig.dpi'] = 500
rcParams['savefig.transparent'] = True
# -- Plot Styles --
rcParams['lines.linewidth'] = 3
navy = (56 / 256, 74 / 256, 143 / 256)
teal = (106 / 256, 197 / 256, 179 / 256)
pink = [199 / 255, 99 / 255, 150 / 255]
rcParams['axes.prop_cycle'] = cycler(color=[teal, navy, pink])
Data Science
Data Visualization
Matplotlib
Python
Data Analysis
Recommended from ReadMedium