目次
散布図を表示する
これまで折れ線グラフと棒グラフの表示方法を解説してきました。
あわせて読みたい


【matplotlib】線の太さ、色、点線[Python]
【線の太さを変更する】 前回、折れ線グラフの全体の見栄えを調整する方法を解説しました。 今度は線自体を色々変更して、見やすいグラフにしてみましょう。 まずは前回…
あわせて読みたい


【matplotlib】棒グラフ[Python]
【棒グラフを表示する】 これまで折れ線グラフに関して解説してきました。 いつもいつも折れ線グラフでは面白くありませんよね。 今回は棒グラフの表示方法を解説してい…
今回は散布図を表示する方法を解説していきます。
前に使った折れ線グラフから散布図を作っていきますので、まずはおさらいから。
%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()
実行結果

散布図へ変更できました。
これを元にして、いろいろいじってみましょう。
点の色を変更する
点の色を変更する方法は、折れ線グラフの時と同じです。
あわせて読みたい


【matplotlib】線の太さ、色、点線[Python]
【線の太さを変更する】 前回、折れ線グラフの全体の見栄えを調整する方法を解説しました。 今度は線自体を色々変更して、見やすいグラフにしてみましょう。 まずは前回…
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()
実行結果

散布図に関しては、こんな感じでしょうか。
次回はエラーバーに関して、解説します。
あわせて読みたい


【matplotlib】エラーバー編[Python]
【データの準備】 これまでmatplotlibに関して、折れ線グラフ、棒グラフ、散布図を解説してきました。 今回はこれらのグラフにエラーバーを表示してみます。 ただこれま…
ということで今回はこんな感じで。
コメント