back to the matplotlib-gallery
at https://github.com/rasbt/matplotlib-gallery
%load_ext watermark
%watermark -u -v -d -p matplotlib,numpy
last updated: 2016-05-13 CPython 3.5.1 IPython 4.0.3 matplotlib 1.5.1 numpy 1.11.0
More info about the %watermark
extension
%matplotlib inline
One of the coolest features added to matlotlib 1.5 is the support for "styles"! The "styles" functionality allows us to create beautiful plots rather painlessly -- a great feature for everyone who though that matplotlib's default layout looks a bit dated!
The styles that are currently included can be listed via print(plt.style.available)
:
import matplotlib.pyplot as plt
print(plt.style.available)
['seaborn-colorblind', 'seaborn-dark', 'seaborn-ticks', 'seaborn-white', 'fivethirtyeight', 'seaborn-poster', 'seaborn-deep', 'dark_background', 'bmh', 'seaborn-muted', 'ggplot', 'seaborn-talk', 'seaborn-darkgrid', 'seaborn-whitegrid', 'grayscale', 'classic', 'seaborn-bright', 'seaborn-notebook', 'seaborn-paper', 'seaborn-dark-palette', 'seaborn-pastel']
Now, there are two ways to apply the styling to our plots. First, we can set the style for our coding environment globally via the plt.style.use
function:
import numpy as np
plt.style.use('ggplot')
x = np.arange(10)
for i in range(1, 4):
plt.plot(x, i * x**2, label='Group %d' % i)
plt.legend(loc='best')
plt.show()
Another way to use styles is via the with
context manager, which applies the styling to a specific code block only:
with plt.style.context('fivethirtyeight'):
for i in range(1, 4):
plt.plot(x, i * x**2, label='Group %d' % i)
plt.legend(loc='best')
plt.show()
Finally, here's an overview of how the different styles look like:
import math
n = len(plt.style.available)
num_rows = math.ceil(n/4)
fig = plt.figure(figsize=(15, 15))
for i, s in enumerate(plt.style.available):
with plt.style.context(s):
ax = fig.add_subplot(num_rows, 4, i+1)
for i in range(1, 4):
ax.plot(x, i * x**2, label='Group %d' % i)
ax.set_xlabel(s, color='black')
ax.legend(loc='best')
fig.tight_layout()
plt.show()