Matplotlib Tutorial: 6. Style Sheets

One common complaint against matplotlib is its relatively ugly default settings. Version 1.4 introduced a nice way to correct for this, using built-in style sheets.

Again, we'll start with the basic inline setting and imports:

In [1]:
%matplotlib inline

from __future__ import print_function, division
import matplotlib.pyplot as plt
import numpy as np

Recall that the default matplotlib plot looks something like this:

In [2]:
x = np.linspace(0, 10)

def plot_curves():
    for i in range(6):
        plt.plot(x, np.sin(x + i), label=str(i));
    plt.ylim(-2, 4)
    plt.legend(ncol=2, frameon=False)
    
plot_curves()

Ugly color combination, and ugly default plot.

Fortunately, in matplotlib 1.4 or newer, we can adjust the style with a single line:

In [3]:
plt.style.use('ggplot')
In [4]:
plot_curves()

Much nicer color choice, and much saner defaults!

The available style sheets can be listed using the following:

In [5]:
print(plt.style.available)
['grayscale', 'bmh', 'dark_background', 'fivethirtyeight', 'ggplot']

Let's take a look at each of these in turn. For each, we'll restore the defaults before activating them

In [6]:
# restore defaults
plt.rcdefaults()
%matplotlib inline

plt.style.use('dark_background')
plot_curves()
In [7]:
# restore defaults
plt.rcdefaults()
%matplotlib inline

plt.style.use('bmh')
plot_curves()
In [8]:
# restore defaults
plt.rcdefaults()
%matplotlib inline

plt.style.use('grayscale')
plot_curves()
In [9]:
# restore defaults
plt.rcdefaults()
%matplotlib inline

plt.style.use('ggplot')
plot_curves()
In [10]:
# restore defaults
plt.rcdefaults()
%matplotlib inline

plt.style.use('fivethirtyeight')
plot_curves()

It is also possible to create your own stylesheets in this way; for more information see http://matplotlib.org/users/style_sheets.html