3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

matplotlibのスタイルシートについて

Last updated at Posted at 2019-01-12

概要

  • matplotlibでスタイルシートが使用できる。

  • 以下のようなメリットがあると考えられる。

    • スタイルシートを呼び出すだけで、任意の設定にできる。
    • グローバル設定を変えなくてすむので柔軟性が高い
    • 個別の図に対してスタイルシートを呼び出せるので、これまた柔軟性が高い。

スタイルシートの登録

  • スタイルシートはグラフの体裁を記載しておくファイル。

  • `plt.style.use('hoge-style')で呼び出すことができる。

  • mpl_configdir/stylelibにスタイルシートを登録できる。

    • ファイル名はhoge-style.mplstyleとする。
    • mpl_configdirmatplotlib.get_configdir()で取得できる。
  • パラメータの記載の方法はmatplotlibrc中の表記に従う。

  • スタイルシートのサンプルは以下の通り。


axes.titlesize : 24
axes.labelsize : 20
lines.linewidth : 3
lines.markersize : 10
xtick.labelsize : 16
ytick.labelsize : 16

  • ある一つの図だけのスタイルシートを変更したい時には、
    with plt.style.context(('hoge-style')):を使用する。

with plt.style.context(('dark_background')):
    plt.plot(np.sin(np.linspace(0, 2 * np.pi)), 'r-o')
plt.show()

その他の方法

rcParamsによる変更

  • matplotlib.rcParamsを変更することで体裁を変更することもできる。
  • この方法は、全ての図に影響を与えるグローバルな設定。

mpl.rcParams['lines.linewidth'] = 2
mpl.rcParams['lines.color'] = 'r'

  • mpl.rcを使用すると、あるカテゴリー(以下ではline)の複数の要素に同時に設定を変更できる。
mpl.rc('lines', linewidth=4, color='g')

おわりに

matplotlibのdocumentが2018年11月に3.0.2になっていました。少し読んだだけでも便利なことが沢山記載されているので、ちゃんと読まなきゃな…

参考

Customizing Matplotlib with style sheets and rcParams

3
7
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
3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?