Customizing plots


  • Know where to look to find out how to tweak plots

  • Be able to prepare a plot for publication

  • Know how to tweak example plots from a gallery for your own purpose

Instructor note

  • 10 min discussion

  • 15 min exercise where we adapt a Matplotlib script

  • 15 min exercise where we try to adapt a gallery example

  • 10 min discussion, Q&A

[this lesson is adapted from]

Styling and customizing plots

  • Do not customize “manually” using a graphical program (not easily repeatable/reproducible).

  • No manual post-processing. This will bite you when you need to regenerate 50 figures one day before submission deadline or regenerate a set of figures after the person who created them left the group.

  • Matplotlib and also all the other libraries allow to customize almost every aspect of a plot.

  • It is useful to study Matplotlib parts of a figure so that we know what to search for to customize things.

  • Matplotlib cheatsheets:

  • You can also select among pre-defined themes/ style sheets, for instance:'ggplot')


Here are 3 exercises where we try to adapt existing scripts to either tweak how the plot looks (exercises 1 and 2) or to modify the input data (example 3).

This is very close to real life: there are so many options and possibilities and it is almost impossible to remember everything so this strategy is useful to practice:

  • select an example that is close to what you have in mind

  • being able to adapt it to your needs

  • being able to search for help

  • being able to understand help request answers (not easy)

Exercise Customization-1: log scale in Matplotlib (15 min)

In this exercise we will learn how to use log scales.

  • To demonstrate this we first fetch some data to plot:

    import pandas as pd
    url = ""
    data = pd.read_csv(url)
    data_2007 = data[data["year"] == 2007]
    • Try the above snippet in a notebook and it will give you an overview over the data.

    • Then we can plot the data, first using a linear scale:

    import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    ax.scatter(x=data_2007["gdpPercap"], y=data_2007["lifeExp"], alpha=0.5)
    ax.set_xlabel("GDP (USD) per capita")
    ax.set_ylabel("life expectancy (years)")

    This is the result but we realize that a linear scale is not ideal here:

    Gapminder data plotted using a linear scale
    • Your task is to switch to a log scale and arrive at this result:

    Gapminder data plotted using log scale
    • What does alpha=0.5 do?

Exercise Customization-2: preparing a plot for publication (15 min)

Often we need to create figures for presentation slides and for publications but both have different requirements: for presentation slides you have the whole screen but for a figure in a publication you may only have few centimeters/inches.

For figures that go to print it is good practice to look at them at the size they will be printed in and then often fonts and tickmarks are too small.

Your task is to make the tickmarks and the axis label font larger, using Matplotlib parts of a figure and web search, and to arrive at this:

Gapminder data plotted with larger font and larger ticks


After the exercises, the group can discuss their findings and it is important to clarify questions at this point before moving on.