Matplotlib タイトルとラベル関数


Matplotlib 参考文档Matplotlib リファレンスドキュメント

Matplotlib は、グラフのタイトル、座標軸ラベル、全体タイトルを設定するための一連の関数を提供しており、グラフを完全に読みやすくするための基礎です。

関連関数

関数機能対応するAxesメソッド
title()Axesのタイトルを設定ax.set_title()
xlabel() / ylabel()x/y軸のラベルを設定ax.set_xlabel() / ax.set_ylabel()
suptitle()Figure全体のタイトルを設定fig.suptitle()
supxlabel() / supylabel()Figureレベルのx/y軸ラベルを設定fig.supxlabel() / fig.supylabel()

関数定義

matplotlib.pyplot.title(label, fontdict=None, loc=None, pad=None,
    *, y=None, **kwargs)
matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, *,
    loc=None, **kwargs)
matplotlib.pyplot.ylabel(ylabel, fontdict=None, labelpad=None, *,
    loc=None, **kwargs)
matplotlib.pyplot.suptitle(t, **kwargs)
matplotlib.pyplot.supxlabel(t, **kwargs)
matplotlib.pyplot.supylabel(t, **kwargs)

よく使うパラメータ

パラメータ説明
label / xlabel / ylabel / tテキスト内容(必須)
fontsize / sizeフォントサイズ、例:14、'large'、'x-large'
fontweight / weightフォントの太さ:'normal'、'bold'、'light'
colorテキストの色
fontdictフォント属性の辞書、例:{'fontsize':14, 'color':'blue'}
loc配置位置:'center'(デフォルト)、'left'、'right'
pad / labelpadテキストと座標軸/グラフ端の間隔(ポイント単位)
y(title専用)タイトルの垂直位置(Axes座標、1.0が上部)

Axesオブジェクトのメソッド名はset_で始まります。例:ax.set_title()、ax.set_xlabel()これはオブジェクト指向インターフェースの推奨される書き方です。


使用例

例1:基本的なタイトルとラベル

実例

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x) * np.exp(-x/5)

fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')

ax.plot(x, y, 'steelblue', linewidth=2)

# タイトルを設定(Axesレベル)
ax.set_title('Damped Sine Wave', fontsize=14, fontweight='bold')

# 軸ラベルを設定
ax.set_xlabel('Time (seconds)', fontsize=12)
ax.set_ylabel('Amplitude (V)', fontsize=12)

ax.grid(True, alpha=0.3)
plt.show()

例2:Figure全体タイトル + 軸ラベル

実例

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig, axes = plt.subplots(2, 2, figsize=(10, 8),
                          layout='constrained')

# 各サブプロットには独自のタイトルがある
axes[0, 0].plot(x, np.sin(x)); axes[0, 0].set_title('sin(x)')
axes[0, 1].plot(x, np.cos(x)); axes[0, 1].set_title('cos(x)')
axes[1, 0].plot(x, np.sin(2*x)); axes[1, 0].set_title('sin(2x)')
axes[1, 1].plot(x, np.cos(2*x)); axes[1, 1].set_title('cos(2x)')

# Figureレベルの全体タイトル
fig.suptitle('Trigonometric Functions Overview',
             fontsize=16, fontweight='bold', y=1.02)

# Figureレベルの軸ラベル(すべてのサブプロットの中央)
fig.supxlabel('x (radians)', fontsize=13)
fig.supylabel('Amplitude', fontsize=13)

plt.show()

例3:タイトルの位置とスタイル

実例

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4),
                                 layout='constrained')

# 左の図:左揃えタイトル
ax1.plot(x, np.sin(x), 'steelblue')
ax1.set_title('Left-aligned Title',
              loc='left',                # 左揃え
              fontsize=12,
              color='steelblue')
ax1.set_xlabel('X-axis', fontsize=10, color='gray')

# 右の図:右揃えタイトル
ax2.plot(x, np.cos(x), 'coral')
ax2.set_title('Right-aligned Title',
              loc='right',               # 右揃え
              fontsize=12,
              color='coral',
              fontstyle='italic')
ax2.set_ylabel('Y-axis', fontsize=10, color='gray',
               rotation=0, labelpad=20)  # ラベルを回転させない

plt.show()

例4:LaTeX数式タイトル

実例

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5, 5, 200)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4),
                                 layout='constrained')

# ガウス分布
sigma, mu = 1.0, 0.0
y1 = 1/(sigma * np.sqrt(2*np.pi)) * np.exp(-(x-mu)**2/(2*sigma**2))
ax1.plot(x, y1, 'steelblue', linewidth=2)

# LaTeX数式をタイトルとして使用
ax1.set_title(
    r'$f(x) = frac{1}{sigmasqrt{2pi}}'
    r'expleft(-frac{(x-mu)^2}{2sigma^2}right)$',
    fontsize=12)

# 多項式
y2 = x**3 - 3*x
ax2.plot(x, y2, 'coral', linewidth=2)
ax2.set_title(r'$f(x) = x^3 - 3x$', fontsize=14)
ax2.axhline(y=0, color='gray', linewidth=0.5)

for ax in [ax1, ax2]:
    ax.set_xlabel('x')
    ax.grid(True, alpha=0.3)

fig.suptitle('Functions with LaTeX Titles', fontsize=14,
             fontweight='bold')
plt.show()
print("サンプル: LaTeX titles displayed")

よくある質問

title() と suptitle() の違いは?

title()(またはax.set_title()):単一のAxesの上にタイトルを設定します。

suptitle()(またはfig.suptitle()):Figure全体の上部に設定され、すべてのサブプロットをカバーする全体タイトルです。

タイトルで中国語をサポートするには?

フォントを設定:plt.rcParams['font.sans-serif'] = ['SimHei'] 或 plt.rcParams['font.family'] = 'sans-serif'。


Matplotlib 参考文档Matplotlib リファレンスドキュメント

その他の拡張