Mplstyle Tutorial

Tutorial introduction to mplstyle stylesheets | February 15, 2017


  • python
  • matplotlib

One of my favorite libraries in Python is matplotlib. Despite the many options we Python programmers have, I still find the simplicity and rawness of matplotlib great to work with. I recently discovered the usefulness of matplotlib stylesheets and how they can help in customizing your plots. In this tutorial, I have laid out the process that I used to get up and running with stylesheets.

Introduction: Matplotlib Stylesheets

A good amount of my workflow involves creating reproducible research in jupyter-notebooks. Notebooks are great for adhoc research as well as for creating publication quality pdf reports or web based dashboards. In fact, I have produced this particular webpage in a notebook.

While matplotlib comes with a lot of different stylesheets, I have run into scenarios where I wanted to create a style that resembled a particular color-scheme. This led me to explore matplotlib stylesheets.

Throughout this post, I will demonstrate how to use matplotlib stylesheets to customize plots for your own needs.

Data Creation

First, let’s create some random data using Pandas and NumPy

import pandas as pd
import numpy as np
rand_data = np.random.randn(1000 * 3).reshape(1000, 3)
returns = pd.DataFrame(rand_data, columns=list("ABC"),
                       index=pd.date_range(start="2016-12-01 08:30:00",
                                           freq="1s", periods=1000))

prices = returns.cumsum() + 100
prices[:10]
A B C
2016-12-01 08:30:00 100.232142 99.682473 99.521759
2016-12-01 08:30:01 99.978837 99.446970 100.423348
2016-12-01 08:30:02 100.756137 100.772167 99.999918
2016-12-01 08:30:03 101.025152 99.063761 100.486253
2016-12-01 08:30:04 99.177024 99.689774 101.648877
2016-12-01 08:30:05 100.318996 99.723182 102.756434
2016-12-01 08:30:06 98.370779 101.317465 101.901374
2016-12-01 08:30:07 98.293057 102.366967 101.768990
2016-12-01 08:30:08 98.319536 102.300582 101.785594
2016-12-01 08:30:09 98.133255 102.308406 103.178428

Basic matplotlib plotting

Let’s plot this data as a time-series keeping out-of-the-box settings for matplotlib.

import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
%matplotlib inline
def plot_ts(df):
    fig, ax = plt.subplots()
    ax.plot_date(df.index.values, prices, '-')
    ax.set_ylabel("Price")
    ax.set_xlabel("Time")
    plt.suptitle("A Time Series Chart")
    plt.show()  
plot_ts(prices)

png

Available stylesheets

matplotlib comes with a few stylesheet options.

seaborn is another well-known plotting package for Python and is built as a wrapper on top of matplotlib. It installs its themes with the matplotlib themes.

Let’s look at the available options for stylesheets.

plt.style.available
[u'seaborn-darkgrid',
 u'seaborn-notebook',
 u'classic',
 u'seaborn-ticks',
 u'grayscale',
 u'bmh',
 u'seaborn-talk',
 u'dark_background',
 u'ggplot',
 u'fivethirtyeight',
 u'seaborn-colorblind',
 u'seaborn-deep',
 u'seaborn-whitegrid',
 u'seaborn-bright',
 u'seaborn-poster',
 u'seaborn-muted',
 u'seaborn-paper',
 u'seaborn-white',
 u'seaborn-pastel',
 u'seaborn-dark',
 u'seaborn',
 u'seaborn-dark-palette']

Using matplotlib stylesheets

We will use the matplotlib themes and see how they change the graph.

ggplot

plt.style.use("ggplot")
plot_ts(prices)

png

bmh

plt.style.use("bmh")
plot_ts(prices)

png

Customization

If one of these options does not work for you, you are in the same boat as I was in one of my research projects.

First thing I did was find a good palette of colors. One great resource to find or create a good pallete is Paletton.

Here is a good article on how and why to choose a good color palette.

Palleton 1

Palleton 12

Using the color-scheme, I have created a new mplstyle document, which you can download from kapilsh.mplstyle.

The document looks like this:


patch.linewidth: 0.5
patch.facecolor: 348ABD
patch.edgecolor: EEEEEE
patch.antialiased: True

font.size: 10.0

axes.facecolor: D5D5D5
axes.edgecolor: black
axes.linewidth: 1
axes.grid: True
axes.titlesize: large
axes.labelsize: medium
axes.labelcolor: black
axes.axisbelow: True

axes.prop_cycle: cycler('color', ['5E4F6D', 'A18271', '4B6B61', 'A19E71', '623F2C', '312142','1D4136', '625F2C'])

xtick.color: black
xtick.direction: out

ytick.color: black
ytick.direction: out

grid.color: white
grid.linestyle: -

figure.facecolor: white
figure.edgecolor: 0.75
figure.figsize: 12, 10
figure.autolayout: False

For the property keys, you can reference your matplotlibrc file. It is typically imstalled in ~/.config/matplotlib/.

Sample plots

Let’s use this stylesheet to create some sample graphs.

plt.style.use("kapilsh.mplstyle")
plot_ts(prices)

png

fig, ax_arr = plt.subplots(ncols=3, nrows=1, sharey=True)
ax = ax_arr[0]
n, bins, patches = ax.hist(returns["A"], 50, normed=1, histtype='stepfilled')
ax.set_ylabel("Frequency")
ax.set_xlabel("Returns")
ax.set_title("A")
ax = ax_arr[1]
n, bins, patches = ax.hist(returns["B"], 50, normed=1, histtype='stepfilled')
ax.set_xlabel("Returns")
ax.set_title("B")
ax = ax_arr[2]
n, bins, patches = ax.hist(returns["C"], 50, normed=1, histtype='stepfilled')
ax.set_xlabel("Returns")
ax.set_title("C")
plt.suptitle("Distribution of Returns")
plt.gcf().set_size_inches(15, 5)
plt.show()

png

returns_sample = returns[:200]

fig, ax = plt.subplots()
ax.plot_date(returns_sample.index.values, returns_sample["A"], '-')
ax.plot_date(returns_sample.index.values, returns_sample["B"], '-')
ax.plot_date(returns_sample.index.values, returns_sample["C"], '-')
ax.set_ylabel("Returns")
ax.set_xlabel("Time")
ax.set_title("Returns Time Series")
plt.gcf().set_size_inches(15, 5)
plt.show()

png

Summary

In this post, I have demonstrated how to use matplotlib stylesheets to customize matplotlib plots.

I used Paletton to create a color-scheme.

Finally, I generated a new mplstyle stylesheet document and showed how to load it into your matplotlib environment to customize plots.

Phone

+1-312-566-7843

Address

------------------------
Chicago, IL 60605
United States of America