Python | ファイル・OS 操作:グラフタイトル

Python Python
スポンサーリンク

概要(グラフタイトルは「何を見せるか」を一瞬で伝える最短手段)

タイトルは、グラフの意味や対象範囲を明確にし、見る人の理解負担を下げます。Matplotlibでは「図全体のタイトル(suptitle)」と「各サブプロットのタイトル(set_title)」があり、サイズ・位置・フォント・改行・補足(サブタイトル)まで細かく調整できます。初心者は「どこにタイトルを付けるか」「見やすいサイズと位置」「動的に内容を差し替える」この3点を押さえると、伝わるグラフになります。


基本の付け方(ここが重要)

単一グラフのタイトル(pyplotスタイル)

import matplotlib.pyplot as plt

x = [1,2,3,4]; y = [100,120,90,130]
plt.plot(x, y)
plt.title("月別売上推移")
plt.xlabel("月"); plt.ylabel("売上(万円)")
plt.tight_layout(); plt.show()
Python

最短はplt.title。ラベルや余白調整(tight_layout)とセットにすると崩れにくいです。

オブジェクト指向スタイルでタイトルを付ける

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(6,4))
ax.plot([1,2,3,4], [100,120,90,130], marker="o")
ax.set_title("月別売上推移")
ax.set_xlabel("月"); ax.set_ylabel("売上(万円)")
fig.tight_layout(); plt.show()
Python

複数サブプロットを使う場面や、細かな調整が必要ならax.set_titleを使います。

図全体のタイトル(suptitle)とサブプロットタイトルの併用

import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6,6), sharex=True)
ax1.plot([1,2,3,4], [100,120,90,130]); ax1.set_title("売上")
ax2.bar(["1月","2月","3月","4月"], [30,25,35,20]); ax2.set_title("利益")
fig.suptitle("四半期レポート(2025Q1)", fontsize=14, y=0.98)
fig.tight_layout(rect=[0, 0, 1, 0.95])  # 図上部にsuptitleの余白を確保
plt.show()
Python

suptitleは「図のまとめ」、set_titleは「各グラフの個別詳細」という役割分担がわかりやすいです。


見やすさの調整(サイズ・位置・フォント・改行)

フォントサイズ・色・位置・余白

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1,2,3,4],[100,120,90,130])

ax.set_title(
    "月別売上推移(暫定)",
    fontsize=13, color="#333333",
    loc="left",         # left/center/right
    pad=10              # タイトルとプロット間の余白(ポイント)
)
fig.tight_layout(); plt.show()
Python

locで左右位置、padでタイトルと軸の距離を調整します。可読性のために色・サイズも整えます。

日本語フォントの設定(文字化け対策)

import matplotlib.pyplot as plt
plt.rcParams["font.family"] = "DejaVu Sans"  # 実環境の日本語フォントに変更推奨

fig, ax = plt.subplots()
ax.plot([1,2,3],[10,20,15])
ax.set_title("日本語タイトル(フォント設定済)")
plt.tight_layout(); plt.show()
Python

環境によっては豆腐(□)になるため、最初に日本語フォントを指定しておくとトラブルが減ります。

複数行タイトル・長文の折り返し

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1,2,3],[10,20,15])

ax.set_title("売上推移(試験運用)\n注:外れ値含む", fontsize=12)
# 長文は改行 \n を使うか、短く要点だけにする
plt.tight_layout(); plt.show()
Python

改行して補足を入れると、サブタイトル的に使えます。長すぎる文は見出し・補足に分けるのが基本。


実務で効く工夫(動的タイトル・注記・保存)

動的に差し込む(f-stringやformat)

import matplotlib.pyplot as plt
month = "2025-12"
total = 420

fig, ax = plt.subplots()
ax.plot([1,2,3,4],[100,120,90,110])
ax.set_title(f"{month} 売上推移(合計: {total} 万円)")
plt.tight_layout(); plt.show()
Python

集計値や期間をタイトルへ差し込むと、グラフ単体でも情報が完結します。

注記(annotation)との使い分け

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1,2,3,4],[100,120,90,130])
ax.set_title("月別売上")
ax.annotate("キャンペーン効果", xy=(2,120), xytext=(3,140),
            arrowprops=dict(arrowstyle="->"))
plt.tight_layout(); plt.show()
Python

タイトルは全体の見出し、annotationは特定点の補足。役割を分けると伝わりやすいです。

画像保存時の余白・解像度

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1,2,3,4],[100,120,90,130])
ax.set_title("月別売上")
fig.tight_layout()
fig.savefig("sales.png", dpi=150, bbox_inches="tight")
Python

保存時はtight_layoutやbbox_inches=”tight”でタイトルが切れないようにします。


サブプロットとレイアウト(複数タイトルの整理)

たくさんの小図に短いタイトルを配する

import matplotlib.pyplot as plt

fig, axes = plt.subplots(2, 2, figsize=(8,6), sharex=True)
titles = ["売上", "利益", "顧客数", "単価"]
for ax, t in zip(axes.ravel(), titles):
    ax.plot([1,2,3,4],[1,2,1.5,2.2])
    ax.set_title(t, fontsize=11)
fig.suptitle("月次ダッシュボード", fontsize=14, y=0.98)
fig.tight_layout(rect=[0, 0, 1, 0.95])
plt.show()
Python

「図全体の大見出し+各小図の短い見出し」で、視線の流れがよくなります。

二段タイトル(メイン+サブ)風の表現

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1,2,3,4],[100,120,90,130])
ax.set_title("月別売上推移", fontsize=14, pad=6)
ax.text(0.5, 1.04, "対象: ECサイト(税込・返品含む)", transform=ax.transAxes,
        ha="center", va="bottom", fontsize=10, color="#555")
fig.tight_layout(); plt.show()
Python

set_titleは短く、textで補足を上側に重ねると“サブタイトル”風にできます。


よくある落とし穴の回避(重なり・位置ズレ・冗長さ)

タイトルやラベルが重なる

  • tight_layoutを呼ぶ。サブプロットではrectでsuptitleの余白を確保する。
  • タイトルが長い場合は改行やサブタイトル分割で短くする。

suptitleが隠れる・はみ出す

  • suptitleのy座標を上げ、tight_layout(rect=…)で上部余白を確保する。
  • 図サイズ(figsize)を広げると余裕が持てる。

冗長なタイトルで意味が伝わりにくい

  • 何を、いつ、どの単位で、どの範囲のデータか。必要最小限にまとめる。
  • 補足は注記(annotation)やサブタイトルで分離する。

例題で身につける(定番から一歩先まで)

例題1:基本タイトルとフォント調整

import matplotlib.pyplot as plt
plt.rcParams["font.family"] = "DejaVu Sans"

fig, ax = plt.subplots()
ax.plot([1,2,3,4],[100,120,90,130])
ax.set_title("月別売上推移", fontsize=13, color="#333", loc="center", pad=8)
fig.tight_layout(); plt.show()
Python

例題2:図全体タイトル+各サブプロットタイトル

import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8,4))
ax1.plot([1,2,3,4],[100,120,90,130]); ax1.set_title("売上")
ax2.bar(["1月","2月","3月","4月"], [30,25,35,20]); ax2.set_title("利益")
fig.suptitle("2025年Q1ダッシュボード", fontsize=14, y=0.98)
fig.tight_layout(rect=[0, 0, 1, 0.95]); plt.show()
Python

例題3:動的情報を差し込む

import matplotlib.pyplot as plt

month = "2025-12"; total = 420
fig, ax = plt.subplots()
ax.plot([1,2,3,4],[100,120,90,110])
ax.set_title(f"{month} 売上推移(合計: {total} 万円)")
fig.tight_layout(); plt.show()
Python

例題4:メイン+サブタイトル・注記の組み合わせ

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1,2,3,4],[100,120,90,130])
ax.set_title("月別売上推移", fontsize=14)
ax.text(0.5, 1.04, "対象: EC / 税込 / 返品含む", transform=ax.transAxes,
        ha="center", va="bottom", fontsize=10, color="#555")
ax.annotate("キャンペーン", xy=(2,120), xytext=(3,140),
            arrowprops=dict(arrowstyle="->"), fontsize=10)
fig.tight_layout(); plt.show()
Python

まとめ

タイトルは「このグラフが何を示すか」を最短で伝える核要素です。単一図にはtitle/set_title、全体にはsuptitleを使い分け、フォント・位置・余白を整える。情報は過不足なく、必要なら改行やサブタイトル、注記で補う。動的に値や期間を差し込めば、グラフ単体でも意味が完結する。tight_layoutと保存時の余白調整を忘れず、冗長さを避ければ、初心者でも“見てわかる”グラフが作れます。

タイトルとURLをコピーしました