5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

matplotlibで散布図を描くサンプル

Last updated at Posted at 2020-12-19

例として「男女グループ別・身長・体重別・読書量」の散布図を描きます。

scatter は、特定の集団の、グループ別、XY別の大きさグラフを描くイメージです。
,

ソース

import matplotlib.pyplot as plt
import pandas as pd

#サンプルデータ
dt={"x身長"       :[ 170,170,150,150,140,140,165,165,155,155] ,
      "y体重"     :[  55, 51, 60, 66, 80, 65, 77, 67, 58, 57] ,
      "s読書量" :[ 700, 67, 80, 30, 55, 34, 45, 10,930,929] ,
       "c性別"    :[   1,  2,  1,  2,  1,  2,  1,  2,  1,  2] 
    }
df=pd.DataFrame(dt)

#描画
fig, ax = plt.subplots()
scatter = ax.scatter(df['x身長'], df['y体重'], c=df['c性別'], s=df['s読書量'], alpha=0.5 )

#タイトル等 
ax.set_xlabel("x身長" , fontsize=15, fontname="MS Gothic")
ax.set_ylabel("y体重" , fontsize=15, fontname="MS Gothic")
ax.set_title('身長 体重 性別別 読書量の分布' , fontname="MS Gothic")

#男女ラベル
legend1=ax.legend(scatter.legend_elements()[0], ["男","女"]   ,  loc="lower left", prop={"family":"MS Gothic"}   ) 
legend1.set_title("c性別"   ,  prop={"family":"MS Gothic"} )
ax.add_artist(legend1)

#読書量ラベル
handles, labels = scatter.legend_elements(prop="sizes",  num=3 , alpha=0.6)
legend2 = ax.legend(handles, labels, loc="upper right",  prop={"family":"MS Gothic"} )
legend2.set_title("s読書量"   ,  prop={"family":"MS Gothic"} )

ax.grid(True)
fig.tight_layout()

plt.show()

結果

image.png

参考

Scatter Demo2
Scatter plot
Scatter plots with a legend
matplotlib.pyplot.scatter
Matplotlibで簡単に日本語を表示する方法(Windows)
凡例の中に日本語タイトルを表示する【matplotlib】

5
2
1

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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?