python:matplotlib:文字の設定
This is an old revision of the document!
概要
matplotlibで使われる文字の大きさやフォントの種類の設定方法を紹介します。
まとめて設定
グラフに登場するすべてのフォントを同じ大きさ、同じフォントにする場合は plt.rcParam を使うと便利です。
- plot.py
import numpy as np import matplotlib.pyplot as plt plt.rcParams["font.family"] = "sans-serif" # 使用するフォント plt.rcParams["font.size"] = 20 # 文字の大きさ x = np.arange(-5,5,0.1) y = np.sin(x) plt.title("graph") plt.xlabel("x") plt.ylabel("y") plt.plot(x, y, label="sin x") plt.legend(loc="lower left") plt.tick_params() plt.tight_layout() plt.show()
個別に設定
フォントのサイズを個別に設定する場合は、各文字の出力のときに fontsize を指定します。
- plot.py
import numpy as np import matplotlib.pyplot as plt x = np.arange(-5,5,0.1) y = np.sin(x) plt.title("graph", fontsize = 20) plt.xlabel("x", fontsize = 20) plt.ylabel("y", fontsize = 20) plt.plot(x, y, label="sin x") plt.legend(loc="lower left", fontsize = 20) plt.tick_params(labelsize = 20) plt.tight_layout() plt.savefig("font.png")
python/matplotlib/文字の設定.1516532918.txt.gz · Last modified: 2021/06/27 21:59 (external edit)