3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

📈 matplotlib チートシート

Last updated at Posted at 2025-05-18

🎨 インポート

import matplotlib.pyplot as plt
# Jupyter Notebook上で、matplotlibのグラフをノートブックの中に表示させるためのおまじない
%matplotlib inline

🧱 基本構文

plt.plot(x, y)
plt.show()

📊 主なグラフの種類

1️⃣ 折れ線グラフ

plt.plot(df["日付"], df["売上"])
plt.title("売上の推移")
plt.xlabel("日付")
plt.ylabel("売上")
plt.grid(True)
plt.show()

2️⃣ 棒グラフ

plt.bar(df["商品"], df["売上"])
plt.title("商品別売上")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

3️⃣ 円グラフ

plt.pie(df["割合"], labels=df["カテゴリ"], autopct='%1.1f%%')
plt.title("カテゴリの割合")
plt.show()

4️⃣ ヒストグラム

plt.hist(df["点数"], bins=10)
plt.title("点数の分布")
plt.xlabel("点数")
plt.ylabel("人数")
plt.grid(True)
plt.show()

5️⃣ 散布図

plt.scatter(df['予算'], df['収益'], alpha=0.2)
plt.title("予算と収益")
plt.show()

6️⃣ 箱ひげ図

plt.boxplot(df['収益'])
plt.title('収益')
plt.show()

🖼 複数グラフの表示(サブプロット)

fig, axs = plt.subplots(1, 2, figsize=(10, 4))  # 1行2列

axs[0].bar(df["商品"], df["売上"])
axs[0].set_title("売上")

axs[1].plot(df["日付"], df["売上"])
axs[1].set_title("売上推移")

plt.tight_layout()
plt.show()

🖌 カスタマイズTips

plt.title("タイトル")
plt.xlabel("X軸ラベル")
plt.ylabel("Y軸ラベル")
plt.legend(["凡例名"])
plt.xticks(rotation=45)
plt.grid(True)
plt.xlim(0, 10)
plt.ylim(0, 100)

📷 グラフを保存する

plt.savefig("graph.png", dpi=300, bbox_inches="tight")

🎁 おまけTips

操作 コマンド
図のサイズ変更 plt.figure(figsize=(10, 5))
軸の調整 plt.xlim(), plt.ylim()
ラベルの回転 plt.xticks(rotation=45)
グリッド追加 plt.grid(True)
透明な背景 plt.savefig("graph.png", transparent=True)

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?