1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで複数のデータを1つのグラフに表示する方法

Posted at

Pythonで複数のデータを1つのグラフに表示する方法

この記事の目的
データ分析において、複数のデータセットを1つのグラフ上に表示することで、データ同士の関係や比較が視覚的にわかりやすくなります。このガイドでは、Pythonのmatplotlibを使用して、複数のデータを1つのグラフに表示するための手法を紹介します。この記事を読むことで、さまざまな形式のデータを一つのグラフやサブプロットで効果的に表現する方法を学べます。


目次

  1. 複数の折れ線グラフを描画する方法
  2. 異なる種類のグラフを組み合わせる方法
  3. サブプロットを使用して複数のグラフを表示する方法
  4. 二軸グラフの作成方法
  5. 参考文献

1. 複数の折れ線グラフを描画する方法

まずは、plt.plot()を複数回使用して、同じグラフ内に異なるデータを表示する方法です。label引数でラベルを付け、凡例を表示することで、どの線がどのデータセットに対応しているかが分かりやすくなります。

import matplotlib.pyplot as plt

# X軸とY軸のデータを用意
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]  # y = x^2
y2 = [1, 2, 3, 4, 5]     # y = x

# 複数の折れ線グラフを描画
plt.plot(x, y1, label='y = x^2')  # y1にラベルを付ける
plt.plot(x, y2, label='y = x')    # y2にラベルを付ける

# グラフの装飾
plt.xlabel('X軸')                 # X軸のラベル
plt.ylabel('Y軸')                 # Y軸のラベル
plt.title('複数の折れ線グラフ')    # グラフのタイトル
plt.legend()                      # 凡例を表示
plt.show()                        # グラフを表示

このコードで、2つの折れ線グラフが1つのグラフ内に描画されます 。


2. 異なる種類のグラフを組み合わせる方法

次に、折れ線グラフと棒グラフなど異なる種類のグラフを組み合わせて1つの図に表示する方法を紹介します。

import matplotlib.pyplot as plt

# X軸と2つの異なるY軸データ
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]  # y = x^2
y2 = [1, 2, 3, 4, 5]     # y = x

# 異なる種類のグラフを描画
plt.bar(x, y1, alpha=0.5, label='棒グラフ')  # 棒グラフを描画
plt.plot(x, y2, color='r', label='折れ線グラフ')  # 折れ線グラフを描画

# グラフの装飾
plt.xlabel('X軸')                   # X軸のラベル
plt.ylabel('Y軸')                   # Y軸のラベル
plt.title('異なるグラフの組み合わせ')  # グラフのタイトル
plt.legend()                        # 凡例を表示
plt.show()                          # グラフを表示

このコードを実行すると、折れ線グラフと棒グラフが重ねて表示されます 。


3. サブプロットを使用して複数のグラフを表示する方法

異なるデータセットを別々のサブプロットで表示したい場合、plt.subplots()を使うことで1つのウィンドウに複数のグラフを配置できます。

import matplotlib.pyplot as plt

# データの用意
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 2, 3, 4, 5]

# サブプロットを作成
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))  # 1行2列のサブプロット

# 各サブプロットにデータを描画
ax1.plot(x, y1)
ax1.set_title('グラフ1')  # サブプロット1のタイトル

ax2.plot(x, y2)
ax2.set_title('グラフ2')  # サブプロット2のタイトル

# レイアウトを調整して表示
plt.tight_layout()
plt.show()

この方法では、同じウィンドウ内に複数のグラフを整然と表示できます 。


4. 二軸グラフの作成方法

二軸グラフを使用すると、異なるスケールのデータを同じX軸に表示できます。これにより、異なる単位や範囲を持つデータも視覚的に比較しやすくなります。

import matplotlib.pyplot as plt

# X軸とY軸のデータ
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]      # y = x^2
y2 = [100, 200, 300, 400, 500]  # 別のスケールのデータ

# 二軸グラフを作成
fig, ax1 = plt.subplots()

# 1つ目のY軸にデータをプロット
ax1.plot(x, y1, 'b-')          # 青色の折れ線グラフ
ax1.set_xlabel('X軸')           # X軸ラベル
ax1.set_ylabel('Y1', color='b') # 1つ目のY軸ラベル

# 2つ目のY軸を追加してプロット
ax2 = ax1.twinx()
ax2.plot(x, y2, 'r-')           # 赤色の折れ線グラフ
ax2.set_ylabel('Y2', color='r') # 2つ目のY軸ラベル

plt.title('二軸グラフ')           # グラフのタイトル
plt.show()                      # グラフを表示

ax1.twinx()を使って2つのY軸を作成し、それぞれのY軸に異なるスケールのデータを描画します 。


参考文献

[1] Tech Academy. "Pythonで複数グラフを描画する方法" https://magazine.techacademy.jp/magazine/39544
[2] Wonderfuru. "matplotlibで複数のグラフを並べて表示する方法" https://wonderfuru.com/maplotlib-%E8%A4%87%E6%95%B0%E3%82%B0%E3%83%A9%E3%83%95%E3%82%92%E4%B8%A6%E3%81%B9%E3%81%A6%E8%A1%A8%E7%A4%BA/
[3] AI研究所. "Pythonで複数のグラフを表示するテクニック" https://ai-kenkyujo.com/programming/python/graph/
[4] AI研究所. "Pythonでの二軸グラフの作成方法" https://ai-kenkyujo.com/programming/language/python/graph/


以上の方法を使うことで、複数のデータセットを一つのグラフ上に表示したり、異なる種類のグラフを組み合わせて視覚的に比較することができます。どの手法を使用するかは、データの性

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?