matplotlib.pyplotとは別に、matplotlib.styleをimportする必要がある。
表示スタイルを変更する場合は、「import matplotlib.style」でmatplotlib.styleモジュールをインポートする必要があります。
使用できる表示スタイルは「matplotlib.style.available」で確認できる。
matplotlib.style.available
▶︎
['Solarize_Light2',
'_classic_test_patch',
'bmh',
'classic',
'dark_background',
'fast',
'fivethirtyeight',
'ggplot',
'grayscale',
'seaborn',
'seaborn-bright',
'seaborn-colorblind',
'seaborn-dark',
'seaborn-dark-palette',
'seaborn-darkgrid',
'seaborn-deep',
'seaborn-muted',
'seaborn-notebook',
'seaborn-paper',
'seaborn-pastel',
'seaborn-poster',
'seaborn-talk',
'seaborn-ticks',
'seaborn-white',
'seaborn-whitegrid',
'tableau-colorblind10']
表示スタイルを変更する場合は「matplotlib.style.use('スタイル名')」と記述する。
本アプリは「fivethirtyeight」の表示スタイルを採用しており、「matplotlib.style.use('fivethirtyeight')」を実行して、Notebook全体に反映させました。
例えば「ggplot」を指定すると、次のような配色になります。
Jypyter Notebookで「matplotlib.style.use('スタイル名')」を実行すると、Notebook全体に反映するため、セル毎に指定する必要はありません。
なお、特定のセルだけ一時的に表示スタイルを変更したい場合は、セルの中で次のようにコンテキストマネージャーを利用して記述します。
with plt.style.context('seaborn-deep'):
plt.plot([3, 1],[1, 3], [4, 2], [2, 4])
with文で表示スタイルを「seaborn-deep」に指定し、with文の中でplot関数を実行することで、このセルだけ「seaborn-deep」が採用されます。