Matplotlib styles examples

The matplotlib documentation is pretty incomplete, and all you really want to know about its style sheets is what they look like - but they're not shown in an easily comparable way. So here they are!

In [49]:
%matplotlib inline

from matplotlib import pyplot as plt
import numpy as np

sorted(plt.style.available)
Out[49]:
['bmh',
 'classic',
 'dark_background',
 'fivethirtyeight',
 'ggplot',
 'grayscale',
 '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']

And what do they look like?

To globally change the style, use plt.style.use(<name>). Here, we're going to temporarily switch between them using the context manager form: with plt.style.context((<name>)). But first let's set up the plots we want to show.

In [50]:
def make_title(s):
    return s.replace('-', ' ').replace('_', ' ').title()


def make_plots(style_str=None):
    fig, axes_array = plt.subplots(2, 2)
    ((ax1, ax2), (ax3, ax4)) = axes_array
    
    # sine and cosine line plots
    x = np.arange(10)
    y_sin = np.sin(x)
    y_cos = np.cos(x)
    ax1.plot(x, y_sin)
    ax1.plot(x, y_cos)
    ax1.set_title('sine and cosine line plots')
    
    # a random scatter plot
    points1 = np.random.rand(20,2)
    points2 = np.random.rand(20,2)
    ax2.scatter(points1[:, 0], points1[:, 1])
    ax2.scatter(points2[:, 0], points1[:, 1])
    ax2.set_title('a random scatter plot')
    
    # a random bar graph
    width = 0.4
    middles = np.arange(1, 6)
    heights1 = np.random.rand(5)
    heights2 = np.random.rand(5)
    ax3.bar(middles - width, heights1, width)
    ax3.bar(middles, heights2, width)
    ax3.set_title('a random bar graph')
    
    # a random pie chart
    values = np.random.rand(5)
    frac_values = values/values.sum()
    labels = ['A', 'B', 'C', 'D', 'E']
    ax4.pie(frac_values, labels=labels)
    ax4.set_title('a random pie chart')
    
    # draw them!
    fig.suptitle('{} ({})'.format(make_title(style_str), style_str) if style_str else 'Default Settings', fontsize=20)
    fig.subplots_adjust(hspace=0.3)
    fig.set_size_inches(10, 10)
    plt.show()

Things you'll note

  • Pie charts don't play nice with style sheet colours
  • Seaborn styles are generally quite nice (note: your matplotlib starts using a seaborn style if you simply import seaborn!)
  • Matlab-like styles make horrible graphs for horrible people
  • None of these plots are really ready for presenting without more tinkering!

Default settings

In [51]:
make_plots()

Seaborn Darkgrid ('seaborn-darkgrid')

In [52]:
with plt.style.context(('seaborn-darkgrid')):
    make_plots('seaborn-darkgrid')        

Seaborn Whitegrid ('seaborn-whitegrid')

In [53]:
with plt.style.context(('seaborn-whitegrid')):
    make_plots('seaborn-whitegrid')        

Seaborn Dark Palette ('seaborn-dark-palette')

In [54]:
with plt.style.context(('seaborn-dark-palette')):
    make_plots('seaborn-dark-palette')        

Seaborn Paper ('seaborn-paper')

In [55]:
with plt.style.context(('seaborn-paper')):
    make_plots('seaborn-paper')        

Seaborn Talk ('seaborn-talk')

In [56]:
with plt.style.context(('seaborn-talk')):
    make_plots('seaborn-talk')        

Seaborn Dark ('seaborn-dark')

In [57]:
with plt.style.context(('seaborn-dark')):
    make_plots('seaborn-dark')        

Seaborn Bright ('seaborn-bright')

In [58]:
with plt.style.context(('seaborn-bright')):
    make_plots('seaborn-bright')        

Ggplot ('ggplot')

In [59]:
with plt.style.context(('ggplot')):
    make_plots('ggplot')        

Grayscale ('grayscale')

In [60]:
with plt.style.context(('grayscale')):
    make_plots('grayscale')        

Seaborn Pastel ('seaborn-pastel')

In [61]:
with plt.style.context(('seaborn-pastel')):
    make_plots('seaborn-pastel')        

Bmh ('bmh')

In [62]:
with plt.style.context(('bmh')):
    make_plots('bmh')        

Seaborn Colorblind ('seaborn-colorblind')

In [63]:
with plt.style.context(('seaborn-colorblind')):
    make_plots('seaborn-colorblind')        

Seaborn Ticks ('seaborn-ticks')

In [64]:
with plt.style.context(('seaborn-ticks')):
    make_plots('seaborn-ticks')        

Fivethirtyeight ('fivethirtyeight')

In [65]:
with plt.style.context(('fivethirtyeight')):
    make_plots('fivethirtyeight')