概要(軸ラベルは「数値の意味」を伝える最短の一言)
軸ラベルは、横軸・縦軸が何を表すのか、単位は何かを一瞬で伝えるための要です。設定はシンプルですが、位置・余白・フォント・単位・目盛りとの整合を丁寧に整えることで、グラフの可読性が劇的に上がります。初心者は「どこにどう付けるか」を押さえ、次に「見やすさの調整」と「複数図(サブプロット)での一貫性」を意識すると、実務品質になります。
基本の付け方(ここが重要)
pyplotスタイルで最短設定
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [100, 120, 90, 130]
plt.plot(x, y)
plt.xlabel("月")
plt.ylabel("売上(万円)")
plt.title("月別売上推移")
plt.tight_layout()
plt.show()
Pythonxlabel と ylabel を書くだけ。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_xlabel("月")
ax.set_ylabel("売上(万円)")
ax.set_title("月別売上推移")
fig.tight_layout()
plt.show()
Python複数サブプロットや細かな調整が必要な場面は ax.set_xlabel / ax.set_ylabel が基本です。
見やすさの調整(余白・位置・フォント・回転)
ラベルと軸の距離や位置を整える
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3,4],[100,120,90,130])
ax.set_xlabel("月", labelpad=10) # 軸との距離(ポイント)
ax.set_ylabel("売上(万円)", labelpad=8)
# 横軸ラベルの位置(center/left/right)、縦軸ラベルの位置(center/bottom/top)
ax.xaxis.set_label_position("right")
ax.yaxis.set_label_position("top")
fig.tight_layout()
plt.show()
Pythonlabelpadで詰めすぎを防ぎ、ラベル位置を変えると混み合う図でも読みやすくできます。
フォント・色・サイズの指定と日本語フォント
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_xlabel("月", fontsize=12, color="#333")
ax.set_ylabel("売上(万円)", fontsize=12, color="#333")
fig.tight_layout()
plt.show()
Python日本語が豆腐(□)になる場合は最初にフォントを設定。色・サイズは落ち着いたトーンで統一すると読みやすいです。
目盛りラベルの回転と書式整形
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(["1月","2月","3月","4月"], [100,120,90,130])
ax.set_xlabel("月")
ax.set_ylabel("売上(万円)")
plt.setp(ax.get_xticklabels(), rotation=30, ha="right") # 長いカテゴリ名に有効
fig.tight_layout()
plt.show()
Python目盛り(tick)自体の見え方が悪いと、軸ラベルがあっても読みにくくなります。回転・配置の調整はセットで行うと効果的です。
実務の勘所(単位・範囲・副軸・サブプロット)
単位を必ず軸ラベルへ明記する
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3,4],[0.95,0.97,0.93,0.98])
ax.set_xlabel("計測回") # 何の軸か
ax.set_ylabel("精度(割合)") # 単位まで
fig.tight_layout(); plt.show()
Python「何の値・どの単位」をラベルに入れると、凡例や注釈がなくても意味が通ります。
軸範囲を合わせてラベルと矛盾しないようにする
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3,4],[10000,15000,12000,18000])
ax.set_xlabel("月")
ax.set_ylabel("売上(円)")
ax.set_ylim(0, 20000) # 範囲を明示し、ラベルと整合
fig.tight_layout(); plt.show()
Python軸範囲が極端だとラベルの印象とズレます。set_xlim/set_ylim で意図を固定しましょう。
副軸(右側)を使う場合のラベル
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
months = ["1月","2月","3月","4月"]
sales = [100,120,130,90]
profit = [30,25,35,20]
ax1.plot(months, sales, color="tab:blue")
ax2.plot(months, profit, color="tab:red")
ax1.set_xlabel("月")
ax1.set_ylabel("売上(万円)", color="tab:blue")
ax2.set_ylabel("利益(万円)", color="tab:red")
fig.tight_layout(); plt.show()
Python副軸では左右でラベル色を系列に合わせると、対応関係が一目で分かります。
複数サブプロットで一貫性を保つ
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8,4), sharex=True)
ax1.plot(["1月","2月","3月","4月"], [100,120,90,130]); ax1.set_ylabel("売上(万円)"); ax1.set_xlabel("月")
ax2.bar(["1月","2月","3月","4月"], [30,25,35,20]); ax2.set_ylabel("利益(万円)"); ax2.set_xlabel("月")
fig.tight_layout(); plt.show()
Python見出しだけでなく、軸ラベルの表現も統一すると、ダッシュボードの理解が速くなります。
よくある落とし穴の回避(重なり・冗長・対数スケール)
ラベルが重なる・切れる問題
- tight_layout を必ず呼ぶ。必要なら fig.tight_layout(rect=…) で余白を確保する。
- ラベルが長いなら略称+注釈、または目盛りを回転・間引き(set_xticks)で対応。
冗長なラベルで情報がぼやける
- 軸ラベルは「何・単位」だけに絞る。期間や集計条件はタイトルや注記へ分離。
対数スケール時の単位の書き方
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_xscale("log")
ax.plot([1,10,100],[1,2,3])
ax.set_xlabel("規模(ログスケール)")
ax.set_ylabel("指標値")
fig.tight_layout(); plt.show()
Python対数スケールを使うなら、ラベルで明記して誤読を防ぐのが礼儀です。
例題で身につける(定番から一歩先まで)
例題1:基本の軸ラベル+目盛り回転
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(["Jan","Feb","Mar","Apr"], [100,120,90,130])
ax.set_xlabel("Month")
ax.set_ylabel("Sales (kJPY)")
plt.setp(ax.get_xticklabels(), rotation=30, ha="right")
fig.tight_layout(); plt.show()
Python例題2:ラベル距離・位置・色を整える
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3,4],[100,120,90,130])
ax.set_xlabel("月", labelpad=12, color="#333")
ax.set_ylabel("売上(万円)", labelpad=10, color="#333")
ax.xaxis.set_label_position("right")
ax.yaxis.set_label_position("top")
fig.tight_layout(); plt.show()
Python例題3:副軸で色合わせ・範囲固定
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
months = ["1月","2月","3月","4月"]
ax1.plot(months, [100,120,130,90], color="tab:blue")
ax2.plot(months, [30,25,35,20], color="tab:red")
ax1.set_xlabel("月")
ax1.set_ylabel("売上(万円)", color="tab:blue"); ax1.set_ylim(0, 160)
ax2.set_ylabel("利益(万円)", color="tab:red"); ax2.set_ylim(0, 60)
fig.tight_layout(); plt.show()
Python例題4:対数スケールとラベル明記
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_xscale("log")
ax.plot([1,10,100,1000],[2,3,4,5], marker="o")
ax.set_xlabel("サンプル数(対数)")
ax.set_ylabel("スコア")
fig.tight_layout(); plt.show()
Pythonまとめ
軸ラベルは「何の値・どの単位」を明確にする最短の一言です。設定は xlabel/ylabel または ax.set_xlabel/ax.set_ylabel で十分ですが、labelpad・位置・フォント・目盛り回転・軸範囲までセットで整えると、可読性が大きく向上します。副軸やサブプロットでは色や表現の一貫性を保ち、対数スケールや単位はラベルで必ず明記する。冗長さを避けて情報を凝縮すれば、初心者でも“伝わる”グラフが安定して作れます。
