0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】Matplotlibでグラフの配色などを表示スタイルで変える場合について

Posted at

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全体に反映させました。
image.png

例えば「ggplot」を指定すると、次のような配色になります。
image.png

Jypyter Notebookで「matplotlib.style.use('スタイル名')」を実行すると、Notebook全体に反映するため、セル毎に指定する必要はありません。

なお、特定のセルだけ一時的に表示スタイルを変更したい場合は、セルの中で次のようにコンテキストマネージャーを利用して記述します。

with plt.style.context('seaborn-deep'):
plt.plot([3, 1],[1, 3], [4, 2], [2, 4])

image.png

with文で表示スタイルを「seaborn-deep」に指定し、with文の中でplot関数を実行することで、このセルだけ「seaborn-deep」が採用されます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?