タイトルを設定する
各グラフにタイトルを設定したい場合はaxes.set_title(),画像全体にタイトルの設定したい場合はfigure.suptitle()で設定できます.
実際に設定してみましょう.
x = np.arange(0, 10, 0.01)
y_1 = [math.cos(i) for i in x]
y_2 = [i**2 for i in x]
fig, ax = plt.subplots(1, 2, figsize = (10, 4))
fig.suptitle('suptitle')
ax[0].plot(x, y_1)
ax[0].set_title('set_title[0]')
ax[1].plot(x, y_2)
ax[1].set_title('set_title[1]')
plt.show()以下で詳しく解説していきます.
個々のグラフにタイトルを設定する
グラフ(Axes)のタイトルを指定するためには,axes.set_title()を使用します.
オプション引数を指定することで,より詳細に決定できます.
label : str (必須)
タイトルとなるテキストfontdict : dict (任意)
タイトルのテキストのフォントサイズや文字色などを変更できます.辞書型のオブジェクトでテキストに関するあらゆるパラメータをまとめています.ちなみに,デフォルトでは以下の設定になっています.rcParamsはグラフ全体のパラメータの設定を管理するオブジェクトです.
タイトルの表示位置.中央,左端,右端の三択.デフォルトではrcParams["axes.titlelocation"]の設定に従い'center'に設定されています.
タイトルの表示位置とグラフ表示領域との間隔.デフォルトではrcParams["axes.titlepad"]の設定に従い6.0に設定されています.
{'fontsize': rcParams['axes.titlesize'],
'fontweight': rcParams['axes.titleweight'],
'color': rcParams['axes.titlecolor'],
'verticalalignment': 'baseline',
'horizontalalignment': loc}
loc : {'center', 'left', 'right'} (任意)
y : float (任意)
タイトルの垂直方向の表示位置.グラフの描写領域の最上部が1.0,最低部が0.0.デフォルトではrcParams["axes.titlelocation"]の設定に従い'None'に設定されています.yが'None'の場合は自動的に位置が決定されます.
pad : float (任意)
x軸を設定する
グラフのx軸を指定するためには,axes.set_xlabel()を使用します.
オプション引数を指定することで,より詳細に決定できます.
xlabel : str (必須)
ラベル名となるテキストlabelpad : float (任意)
タイトルの表示位置とグラフ表示領域との間隔.デフォルトではrcParams["axes.labelpad"]の設定に従い4.0に設定されています.loc : {'center', 'left', 'right'} (任意)
タイトルの表示位置.中央,左端,右端の三択.デフォルトではrcParams["xaxis.labellocation"]の設定に従い'center'に設定されています.y軸を設定する
オプション引数を指定することで,より詳細に決定できます.
ylabel : str (必須)
ラベル名となるテキストlabelpad : float (任意)
ラベルの表示位置とグラフ表示領域との間隔.デフォルトではrcParams["axes.labelpad"]の設定に従い4.0に設定されています.loc : {'bottom', 'center', 'top'} (任意)
ラベルの表示位置.下,中央,上の三択.デフォルトではrcParams["yaxis.labellocation"]の設定に従い'center'に設定されています.テキストのパラメータ変更
フォント・フォントサイズ・文字色・表示位置などが変更方法について解説していきます.
個別に設定する
関数の引数で直接パラメータを指定します.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [9, 19, 24, 30, 48, 54, 67, 70, 85, 94]
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('tilte')
ax.set_xlabel('x lim', fontstyle = 'italic', fontfamily = 'Times New Roman', fontsize = 24, color = 'red', labelpad = 20, loc = 'right')
ax.set_ylabel('y lim')
plt.show()
plt.close('all')保存した設定を適用させる
辞書型オブジェクトにパラメータを保存し,引数として渡すことで指定します.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [9, 19, 24, 30, 48, 54, 67, 70, 85, 94]
xlim_setting_dict = {
'fontfamily' : 'Times New Roman',
'fontstyle' : 'italic',
'fontsize' : 24,
'color' : 'red',
'labelpad' : 20,
'loc' : 'right'
}
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('tilte')
ax.set_xlabel('x lim', **xlim_setting_dict)
ax.set_ylabel('y lim')
plt.show()
plt.close('all'))まとめて設定する
rcParamsで共通パラメータを変更します.変更後は個別に指定しなくても全てのパラメータが変更されています.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [9, 19, 24, 30, 48, 54, 67, 70, 85, 94]
mpl.rcParams['font.family'] = 'Times New Roman'
mpl.rcParams['axes.labelsize'] = 24
mpl.rcParams['font.style'] = 'italic'
mpl.rcParams['axes.labelcolor'] = 'red'
mpl.rcParams['axes.labelpad'] = 20
mpl.rcParams['xaxis.labellocation'] = 'right'
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('tilte')
ax.set_xlabel('x lim')
ax.set_ylabel('y lim')
plt.show()
plt.close('all')テキストパラメータ詳細
fontsize : フォント
日本語フォントを用いれば日本語でラベルが書ける.デフォルトでは日本語未対応.以下代表的なフォント
- MS ゴシック : 'MS Gothic'
- MS 明朝体 : 'MS Mincho'
- 游ゴシック : 'Yu Gothic'
- 游明朝 : 'Yu Mincho'
- Times New Roman : 'Times New Roman'
- Century : 'Century'
- Arial : 'Arial'
fontstyle : スタイル
フォントのスタイル
- 通常 : ‘normal’
- イタリック体 : ‘italic’
- 斜体 : ‘oblique’
fontsize : フォントサイズ
フォントのサイズ.デフォルトは12.テキストで指定する方法と数値で指定する方法がある.- xx-small : 5.79
- x-small : 6.94
- small : 8.33
- medium : 10.0
- large : 12.0
- x-large : 14.4
- xx-large : 17.28
- larger : 12.0
- smaller : 8.33
color : 文字色
色の名前で指定する方法.
(公式ページより)
16進数のカラーコードで指定する
| 色(color) | カラー名 (color name) | カラーコード (color code) | 色(color) | カラー名 (color name) | カラーコード (color code) |
|---|---|---|---|---|---|
| white | #ffffff | green | #008000 | ||
| silver | #c0c0c0 | aqua | #00ffff | ||
| gray | #808080 | teal | #008080 | ||
| black | #000000 | blue | #0000ff | ||
| red | #ff0000 | navy | #000080 | ||
| maroon | #800000 | fuchsia | #ff00ff | ||
| yellow | #ffff00 | purple | #800080 | ||
| olive | #808000 | orange | #ffa500 | ||
| lime | #00ff00 |
.png)
0 件のコメント:
コメントを投稿