How to Change Matplotlib Plot’s Style

How to Change Matplotlib Plot Style to Colorblind friendly?
How to Set Matplotlib Plot Style to Seaborn Colorblind Style

In this post we will learn how to find all available style options for matplotlib plot themes and learn to set a style for matplotlib plot. To illustrate the styling options available in Matplotlib, we will use histogram made from beta distributions.

To get started, let us load the modules needed.

import matplotlib.pyplot as plt
import numpy as np

We can use style or theme options in matplotlib by importing style module from matplotlib.

from matplotlib import style

How to list all available style options in Matplotlib

Matplotlib has 26 different themes or styles available for making plot. If you are interested in finding out all 26 available style options, plt.style.available command will give the names of all available styles as a list.

# get the list of style options in Matplotlib
plt.style.available

['Solarize_Light2',
 '_classic_test_patch',
 'bmh',
 'classic',
 'dark_background',
 'fast',
 'fivethirtyeight',
 'ggplot',
 'grayscale',
 'seaborn',
 'seaborn-bright',
 'seaborn-colorblind',
 'seaborn-dark',
 'seaborn-dark-palette',
 'seaborn-darkgrid',
 'seaborn-deep',
 'seaborn-muted',
 'seaborn-notebook',
 'seaborn-paper',
 'seaborn-pastel',
 'seaborn-poster',
 'seaborn-talk',
 'seaborn-ticks',
 'seaborn-white',
 'seaborn-whitegrid',
 'tableau-colorblind10']

Matplotlib Default Style

When we make plot with Matplotlib, by default it uses default style. Here is an example, three histograms of beta distributions, of a plot with default style.

fig, ax = plt.subplots()
ax.hist(np.random.beta(10, 10, size=1000),
        bins=25, 
        alpha=0.8)
ax.hist(np.random.beta(10, 1, size=1000),
        bins=25, 
        alpha=0.8)
ax.hist(np.random.beta(1, 10, size=1000),
        bins=25, 
        alpha=0.8)
ax.set_title("default Matplotlib style")
plt.savefig("default_matplotlib_style_Python.png",
                    format='png',dpi=150)

Matplotlib Default Plot Style

How to set a Matplotlib Style to a plot

We can set a style for plot made with matplotlib using “plt.style.use()” function with the style name of interest. This will change the look or the theme of a plot. For example, to set style to “seaborn-colorblind”, we use the following statement before making the plot.

plt.style.use('seaborn-colorblind')

Let us go ahead and simplify making the histogram using hist() function by wrapping it as a function.

def plot_beta_hist(ax, a, b):
    ax.hist(np.random.beta(a, b, size=1000),
        bins=25, 
        alpha=0.8)

Set Matplotlib PLot Style: Example 1

Now we can use plt.style.use(‘seaborn-colorblind’) to change the style and plot histograms using the above function.

plt.style.use('seaborn-colorblind')
fig, ax = plt.subplots()
plot_beta_hist(ax, 10, 10)
plot_beta_hist(ax, 10, 1)
plot_beta_hist(ax, 1, 10)
ax.set_title("Seaborn colorblind style sheet")
plt.savefig("How_to_set_matplotlib_style_to_Seaborn_colorblind_Python.png",
                    format='png',dpi=150)

We have successfully changed the matplotlib style from default style to colorblind friendly Seaborn-colorblind theme.

How to Set Matplotlib Plot Style to Seaborn Colorblind Style

Set Matplotlib Plot Style: Example 2

Similarly, here is another example, where we use ggplot style for a plot made with maptplotlib using “plt.style.use(‘ggplot’)” statement before making the plot.

plt.style.use('ggplot')
fig, ax = plt.subplots()
plot_beta_hist(ax, 10, 10)
plot_beta_hist(ax, 10, 1)
plot_beta_hist(ax, 1, 10)
ax.set_title("ggplot style sheet")
plt.savefig("How_to_set_matplotlib_style_to_ggplot_style_Python.png",
                    format='png',dpi=150)

Our plot made with ggplot style in Matplotlib looks like this.

How to Change Matplotlib Plot Style to ggplot grey theme

Note that when we change the plot style, it persists through the session. To reset the plot style, we can use plt.style.use(‘default’).