back to thematplotlib-gallery
athttps://github.com/rasbt/matplotlib-gallery
%load_ext watermark
%watermark -u -v -d -p matplotlib,numpy
last updated: 2016-05-13 CPython 3.5.1IPython 4.0.3matplotlib 1.5.1numpy 1.11.0
%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 viaprint(plt.style.available)
:
importmatplotlib.pyplotaspltprint(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 theplt.style.use
function:
importnumpyasnpplt.style.use('ggplot')x=np.arange(10)foriinrange(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 thewith
context manager, which applies the styling to a specific code block only:
withplt.style.context('fivethirtyeight'):foriinrange(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:
importmathn=len(plt.style.available)num_rows=math.ceil(n/4)fig=plt.figure(figsize=(15,15))fori,sinenumerate(plt.style.available):withplt.style.context(s):ax=fig.add_subplot(num_rows,4,i+1)foriinrange(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()