Movatterモバイル変換


[0]ホーム

URL:


  1. matplotlib-gallery
  2. ipynb
Notebook

Sebastian Raschka

back to thematplotlib-gallery athttps://github.com/rasbt/matplotlib-gallery

In [1]:
%load_ext watermark
In [2]:
%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

In [3]:
%matplotlib inline


Matplotlib Formatting IV: Style Sheets

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!



Sections

The styles that are currently included can be listed viaprint(plt.style.available):

In [8]:
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:

In [6]:
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:

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

In [56]:
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()
In [ ]:

[8]ページ先頭

©2009-2025 Movatter.jp