back to the matplotlib-gallery
at https://github.com/rasbt/matplotlib-gallery
In [1]:
%load_ext watermark
In [2]:
%watermark -u -v -d -p matplotlib,numpy
[More info](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/ipython_magic/watermark.ipynb) about the `%watermark` extension
In [3]:
%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)
:
In [8]:
import matplotlib.pyplot as plt
print(plt.style.available)
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:
In [6]:
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:
In [7]:
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:
In [56]:
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()
In [ ]: