Matplotlibでこのように図を作成しています。
from matplotlib import pyplot as plt
fig = plt.figure()
plt.plot(data)
fig.suptitle('test title')
plt.xlabel('xlabel')
plt.ylabel('ylabel')
fig.savefig('test.jpg')
図のタイトルと軸ラベルのフォントサイズを指定したいと思います。3つとも異なるフォントサイズにする必要があるので,グローバルなフォントサイズを設定する(mpl.rcParams['font.size']=x
)のは,私が望むものではありません。図のタイトルと軸ラベルのフォントサイズを個別に設定するにはどうしたらいいですか?
label,
title, などのようなテキストを扱う関数は、[matplotlib.text.Text][1]と同じパラメータを受け入れます。フォントサイズについては,
size/fontsize`を使うことができます.
from matplotlib import pyplot as plt
fig = plt.figure()
plt.plot(data)
fig.suptitle('test title', fontsize=20)
plt.xlabel('xlabel', fontsize=18)
plt.ylabel('ylabel', fontsize=16)
fig.savefig('test.jpg')
タイトルと
ラベルのサイズをグローバルに設定するために、[
mpl.rcParams][2]は
axes.titleizeと
axes.labelsize` を含みます。(ページより)。
axes.titlesize : large # fontsize of the axes title
axes.labelsize : medium # fontsize of the x any y labels
(私が見る限り、x
とy
のラベルサイズを別々に設定する方法はありません)。
また、axes.titleize
はsuptitle
には影響しないようですね。手動で設定する必要があると思います。
また、rcParamsの辞書を使ってグローバルに行うこともできます。
import matplotlib.pylab as pylab
params = {'legend.fontsize': 'x-large',
'figure.figsize': (15, 5),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
pylab.rcParams.update(params)
もしあなたが ax
オブジェクトを使ってプロットすることに慣れているのであれば、ax.xaxis.label.set_size()
を覚えておくと楽かもしれませんし、少なくとも ipython のターミナルで tab を使って探すのは簡単だと思います。効果を見るためには、後に再描画の操作が必要なようです。例えば、以下のようになります。
import matplotlib.pyplot as plt
# set up a plot with dummy data
fig, ax = plt.subplots()
x = [0, 1, 2]
y = [0, 3, 9]
ax.plot(x,y)
# title and labels, setting initial sizes
fig.suptitle('test title', fontsize=12)
ax.set_xlabel('xlabel', fontsize=10)
ax.set_ylabel('ylabel', fontsize='medium') # relative to plt.rcParams['font.size']
# setting label sizes after creation
ax.xaxis.label.set_size(20)
plt.draw()
スーパータイトルのサイズを作成後に設定する同様の方法を知りません。
タイトルのフォント(軸のフォントではなく)だけを変更するには、以下のようにしました。
import matplotlib.pyplot as plt
fig = plt.Figure()
ax = fig.add_subplot(111)
ax.set_title('My Title', fontdict={'fontsize': 8, 'fontweight': 'medium'})
matplotlib.text.Text][1]のすべてのkwargsを除くfontdict。
[1]: https://matplotlib.org/api/text_api.html#matplotlib.text.Text
set_ylabel()の前に
right_ax` を配置する。
ax.right_ax.set_ylabel('ABスケール' )
となります。