Matplotlibの図のタイトル

Matplotlibの図のタイトル#

公開日

この記事では、Matplotlibのグラフタイトルの設定方法を説明します。 Matplotlibのグラフでは、1つのFigureの内部に、1つ以上のサブプロットを表示できます。 このFigureとサブプロットのそれぞれにタイトルを表示できます。

Figureにタイトルを設定する場合、Figure.suptitle()メソッドを使用します。 一方、サブプロットにタイトルを設定する場合、Axes.set_title()メソッドを使用します。 以下に例を示します。

import matplotlib.pyplot as plt

x = [5, 1, 4, 3]

fig, ax = plt.subplots()
ax.plot(x)
fig.suptitle("Figure title")
ax.set_title("Subplot title")
plt.show()
../_images/120a70c840bb1080e498a682a8dad9f0d2ef28fb069e762da5f34eca1a832a82.png

複数のサブプロットにタイトルを設定する例を以下に示します。

fig, ax = plt.subplots(nrows=2, ncols=2)
for i in range(2):
    for j in range(2):
        ax[i,j].plot(x)
        ax[i,j].set_title(f"Subplot title {i}, {j}")

fig.suptitle("Figure title")
fig.tight_layout()
plt.show()
../_images/8b8046f8656afb79ce9281d8eabb53fdc0d8023a56449cca77853eeb7b8819c9.png

Figureのタイトル#

Figureにタイトルを設定するFigure.suptitle()メソッドの主なオプションを以下に示します。

  • x (float): タイトルのx座標(デフォルト値:0.5)

  • y (float): タイトルのy座標(デフォルト値:0.98)

  • horizontalalignment, ha (str): タイトルのx方向の位置(center(デフォルト), left, right)

  • verticalalignment, va (str): タイトルのy方向の位置(top(デフォルト), center, bottom, baseline)

  • fontsize, size (float/str): フォントサイズ。数値またはxx-small, x-small, small, smaller, medium, large(デフォルト), x-large, xx-large

  • fontweight, weight (float/str): フォントの太さ。数値(0-1000)またはultralight, light, normal(デフォルト), regular, book, medium, roman, semibold, demibold, demi, bold, heavy, extra bold, black

タイトルを左寄せの太字にした例を以下に示します。

fig, ax = plt.subplots()
ax.plot(x)
fig.suptitle("Figure title", horizontalalignment="left", x=0.2, weight="bold")
ax.set_title("Subplot title")
plt.show()
../_images/3ed4cae8f45d95288426a77198ec847ec5fbbc7535b0e0f5566190b2ce1bafb7.png

Figureのタイトルをグラフの下側に表示するには、yオプションを用います。

fig, ax = plt.subplots()
ax.plot(x)
fig.suptitle("Figure title", y=0)
ax.set_title("Subplot title")
plt.show()
../_images/21ece7b768c9e3e8d247ebd90e592043955393b18112cca1faf7be7975b6db2d.png

サブプロットのタイトル#

サブプロットにタイトルを設定するAxes.set_title()メソッドの主なオプションを以下に示します。

  • fontdict (dict): タイトルの設定。辞書のキーは以下の通り。

    • fontsize, size (float/str): フォントサイズ。数値またはxx-small, x-small, small, smaller, medium, large(デフォルト), x-large, xx-large

    • fontweight, weight (float/str): フォントの太さ。数値(0-1000)またはultralight, light, normal(デフォルト), regular, book, medium, roman, semibold, demibold, demi, bold, heavy, extra bold, black

    • color (str): 文字の色

    • horizontalalignment, ha (str): タイトルのx方向の位置(center(デフォルト), left, right)

    • verticalalignment, va (str): タイトルのy方向の位置(top(デフォルト), center, bottom, baseline)

  • loc (str): 文字のx方向の位置(center(デフォルト), left, right)

  • y (float): タイトルのy座標(1がトップ。デフォルト値:None

  • pad (float): Axes上部からのオフセット(デフォルト値:6.0

タイトルを左寄せの太字にした例を以下に示します。

fig, ax = plt.subplots()
ax.plot(x)
fig.suptitle("Figure title")
ax.set_title("Subplot title", fontdict={"weight": "bold"}, loc="left")
plt.show()
../_images/2907202ce88221716406cf2d1814f8e6ad10f1a58596262de772309a285b6f21.png

サブプロットのタイトルをグラフの下側に表示するには、yオプションを用います。

fig, ax = plt.subplots()
ax.plot(x)
fig.suptitle("Figure title")
ax.set_title("Subplot title", y=-0.2)
plt.show()
../_images/0f8ffa17227319831be9d5c0adf485d327473571fa8165a5a283fc3701644724.png