Python Friday #182: Style Your Plots in Seaborn

Seaborn is a powerful library that we can use to explore data. Once we have figured out what we want to show, it is time to adapt the plots to our requirements. In this post we explore the options we have for styling our plots.

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.

 

Change the colours

One of the most important changes I like to make is to select a different colour for the plots. Seaborn offers us colour palettes that have predefined colours for our graphs. Unfortunately, I could not find a page that offers a nice list of all pre-defined palettes.

The best thing I found is the post Seaborn Color Palettes and How to Use Them , which offers much more details than Choosing color palettes in the official documentation.

This gives us a small sample of the colours:

3 examples of distinct colours and 2 examples with a gradient of colours

The parameter as_cmap=True in combination with the sequential colormaps “rocket“, “mako“, “flare“, and “crest” gives us a good colour range for numerical values. If we add a “_r” at the end of the name, we get the colour range reversed.

We can use a palette in our graphs with the palette parameter:

The pair plot now uses the pastel palette

If we want to know what colours are in a palette, we can get their hex codes with this command:

[‘#a1c9f4’, ‘#ffb482’, ‘#8de5a1’, ‘#ff9f9b’, ‘#d0bbff’, ‘#debb9b’, ‘#fab0e4’, ‘#cfcfcf’, ‘#fffea3’, ‘#b9f2f0’]

 

Custom palettes

If we want to use a custom set of colours, we can create our own palette by defining the values we want:

We then can use it with like the pre-defined palettes, but be aware that we need to assign the variable and not a predefined string (so no ” around the name):

The pair plot now uses our custom palette

 

Add a title

We can add a title to our graphs with the set() method that we can apply on graphs and legends:

This allows us to specify one title for the graph as a whole and a specific title for the legend:

A graph with a title for the graph and a title for the legend

If we do not want to see the legend, we can set legend to False:

The gives us a graph without a legend:
A graph without a legend

 

Themes

Themes offer a collection of settings that we can apply in one go. We can choose between these themes:

  • white (with an empty/white background)
  • dark (with a dark background)
  • ticks (the white template but with ticks for the values)
  • whitegrid (the white template with grid lines)
  • darkgrid (the dark template with grid lines)[default]

Important: When we set a template, it will change the settings of all Seaborn and Matplotlib plots.

We can use the template with the set_theme() method:

This gives us the same data in plots using different themes:

The 5 themes offer a slightly different presentation of our data.

 

Set a context

Seaborn offers us another way to change the appearance of our plots. We can choose between one of the 4 pre-defined contexts:

  • notebook with optimisations to work on a computer / Jupyter Notebook (the default in Jupyter).
  • paper has smaller fonts and a slightly thinner line than notebook.
  • talk is for presentations with bigger fonts and stronger lines.
  • poster is optimised to show the most important parts from further away.

If we want to change all plots, we can use the method set_context(). In contrast to templates, we can change the context for only one plot and use the method plotting_context() in a with block:

This gives us the same graph in 4 different contexts:

The context influences how the graph is drawn.

If we like a style but want to make a small adjustment, we can use the plotting_context() method to get the settings for the current style:

We then can use the parameter rc with a dictionary of the values we want to change or use the font_scale parameter to change the font size:

This gives us a slightly modified notebook context:
The notebook context with a thicker line and a stronger font.

 

Next

This post only shows you a few of the many ways you can influence how Seaborn creates its graphs. I suggest you try these ones first and then dig deeper into the styling options of Seaborn.

Next week we explore a few advanced tips and tricks for JupyterLab that can help you should you get stuck.

Leave a Comment

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