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 [3]:
%matplotlib inline

from matplotlib import pyplot as plt
import numpy as np


And what do they look like?

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

In [4]:
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) - width, heights1, width), 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.set_size_inches(10, 10)

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 [5]: