【matplotlib】:散布図[Python]

  • URLをコピーしました!
目次

散布図を表示する

これまで折れ線グラフと棒グラフの表示方法を解説してきました。

今回は散布図を表示する方法を解説していきます。

前に使った折れ線グラフから散布図を作っていきますので、まずはおさらいから。

%matplotlib notebook

from matplotlib import pyplot as plt

y1_value = [1, 2, 4, 8, 16, 32, 64, 128, 256, 1028]
x1_value = range(1, len(y_value)+1)

y2_value = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
x2_value = range(1, len(y2_value)+1)

fig=plt.figure(figsize=(10,6))

plt.plot(x1_value, y1_value, label="test1")
plt.plot(x2_value, y2_value, label="test2")

plt.title("Test Graph", {"fontsize": 20})
plt.xlabel("Numbers", {"fontsize": 20})
plt.ylabel("Value", {"fontsize": 20})
plt.tick_params(labelsize=20)
plt.legend(prop={"size": 20}, loc="best")
plt.grid()

plt.show()

実行結果

これを散布図に変更するには、plt.plot()をplt.scatter()に変更します。

%matplotlib notebook

from matplotlib import pyplot as plt

y1_value = [1, 2, 4, 8, 16, 32, 64, 128, 256, 1028]
x1_value = range(1, len(y_value)+1)

y2_value = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
x2_value = range(1, len(y2_value)+1)

fig=plt.figure(figsize=(10,6))

plt.scatter(x1_value, y1_value, label="test1")
plt.scatter(x2_value, y2_value, label="test2")

plt.title("Test Graph", {"fontsize": 20})
plt.xlabel("Numbers", {"fontsize": 20})
plt.ylabel("Value", {"fontsize": 20})
plt.tick_params(labelsize=20)
plt.legend(prop={"size": 20}, loc="best")
plt.grid()

plt.show()

実行結果

散布図へ変更できました。

これを元にして、いろいろいじってみましょう。

点の色を変更する

点の色を変更する方法は、折れ線グラフの時と同じです。

plt.scatter()の中に、color=”色”で変更でき、色の指定の方法は、色の名前、色の名前の略、カラーコードで指定できます。

%matplotlib notebook

from matplotlib import pyplot as plt
import numpy as np

y1_value = [1, 2, 4, 8, 16, 32, 64, 128, 256, 1028]
x1_value = range(1, len(y_value)+1)

y2_value = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
x2_value = range(1, len(y2_value)+1)

fig=plt.figure(figsize=(10,6))

plt.bar(np.array(x1_value)-0.15, y1_value, color="red", width=0.3, label="test1")
plt.bar(np.array(x2_value)+0.15, y2_value, color="blue", width=0.3, label="test2")

plt.title("Test Graph", {"fontsize": 20})
plt.xlabel("Numbers", {"fontsize": 20})
plt.ylabel("Value", {"fontsize": 20})
plt.tick_params(labelsize=20)
plt.legend(prop={"size": 20}, loc="best")
plt.grid()

plt.show()

実行結果

点の大きさを変更する

点の大きさを変更するには、plt.scatter()にs=”大きさ”で変更できます。

%matplotlib notebook

from matplotlib import pyplot as plt

y1_value = [1, 2, 4, 8, 16, 32, 64, 128, 256, 1028]
x1_value = range(1, len(y_value)+1)

y2_value = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
x2_value = range(1, len(y2_value)+1)

fig=plt.figure(figsize=(10,6))

plt.scatter(x1_value, y1_value, color="red", s=100, label="test1")
plt.scatter(x2_value, y2_value, color="blue", s=100,  label="test2")

plt.title("Test Graph", {"fontsize": 20})
plt.xlabel("Numbers", {"fontsize": 20})
plt.ylabel("Value", {"fontsize": 20})
plt.tick_params(labelsize=20)
plt.legend(prop={"size": 20}, loc="best")
plt.grid()

plt.show()

実行結果

点の形を変更する

点の形を変更するには、plt.scatter()の中に、marker=”点の形”で指定します。

指定できる点の形に関しては、たくさんあり、流石に全部は紹介できないので、公式サイトのリンクを貼っておきます。

%matplotlib notebook

from matplotlib import pyplot as plt

y1_value = [1, 2, 4, 8, 16, 32, 64, 128, 256, 1028]
x1_value = range(1, len(y_value)+1)

y2_value = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
x2_value = range(1, len(y2_value)+1)

fig=plt.figure(figsize=(10,6))

plt.scatter(x1_value, y1_value, color="red", s=100, marker="^", label="test1")
plt.scatter(x2_value, y2_value, color="blue", s=100, marker="X", label="test2")

plt.title("Test Graph", {"fontsize": 20})
plt.xlabel("Numbers", {"fontsize": 20})
plt.ylabel("Value", {"fontsize": 20})
plt.tick_params(labelsize=20)
plt.legend(prop={"size": 20}, loc="best")
plt.grid()

plt.show()

実行結果

散布図に関しては、こんな感じでしょうか。

次回はエラーバーに関して、解説します。

ということで今回はこんな感じで。

よかったらシェアしてね!
  • URLをコピーしました!

コメント

コメントする

目次