LoginSignup
6
5

More than 5 years have passed since last update.

jupyterでmatplotlibのrcParamsの設定を最初のセルに書くと無視される

Posted at

jupyterとmatplotlibを使ってグラフを作ることは多くの人がやると思う。
そのグラフの表示をカスタマイズするためにplt.rcParamsを変更する方法があるが、この変更を最初のセルで行うと設定変更が適用されない。

どうやらjupyterのバグらしい。
https://github.com/jupyter/notebook/issues/3385

rcParamsとは何かということについては例えば以下の記事が参考になる。
- https://qiita.com/hysterect/items/67c94a62ae0a0c258449
- http://cyanatlas.hatenablog.com/entry/2018/06/20/144937

動作確認したバージョン

  • Python 3.7.0
  • jupyter 1.0.0
  • jupyter-core 4.4.0
  • jupyter-console 6.0.0
  • jupyter-client 5.2.3
  • matplotlib 3.0.1

問題となるケース

最初のセルでmatplotlibをimportして、rcParamsを設定する。

最初のセルに以下を入力。

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.size'] = 20   # フォントを大きくする

2つ目のセルに以下を入力。

x = np.arange(-3.0, 3.0, 0.01)
plt.plot( x, np.sin(x) )

実行すると以下の様になりフォントが小さいまま。

image.png

回避方法

このバグはrcParamsの設定を2つ目以降のセルで行うことで回避できる。以下の様にimportとrcParamsの設定を別のセルで行うと、rcParamsが適用されフォントの大きさが変わった。

image.png

6
5
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
6
5