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/5cb72176c00155b1b31a7b2ce3dbbd654e19854d4940690f8e10032b4f9bea55.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/80eb5155efccd3cbb81ad08bc9cd39b9ee88d5d118e7c6fc1f5fe603d3420f67.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/db096c3661d53d12b4bda4b78a721da704f2fb0275730751f6c70e5087401f6b.png

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

fig, ax = plt.subplots()
ax.plot(x)
fig.suptitle("Figure title", y=0)
ax.set_title("Subplot title")
plt.show()
../_images/09e8c519ab9f4b03ba18000f8a2bccf4588bc54c7497c017e966a4745c0b07f0.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/7952f54c8ea12a71c60496100443d4ab811f6d74683c50b1c69beac5544da94d.png

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

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