【Pythonデータサイエンス超入門1-3】Seabornによるグラフ作成の基本【YouTube】
YouTubeによるPythonデータサイエンス超入門シリーズ「Seabornによるグラフ作成の基本」で紹介したソースコードを掲載しているページです。
ご自由に動画で使用したCSVのダウンロードやソースコードのコピー&ペーストなどしてご活用ください。
YouTubeチャンネル
動画リンク
CSVのダウンロード
ソースコード
折れ線グラフ
1 2 3 4 5 6 |
df_weather = pd.read_csv("weather.csv",index_col="年月日",parse_dates=True) sns.set(font="Hiragino Maru Gothic Pro",context="poster",style="white") plt.figure(figsize=(15,8)) plt.xticks(rotation=90) sns.lineplot(data=df_weather[["平均気温(℃)","降水量の合計(mm)"]]) plt.savefig("折れ線.png") |
棒グラフ
1 2 3 4 |
df_score = pd.read_csv("test_score.csv") sns.set(font="Hiragino Maru Gothic Pro",context="talk") plt.figure(figsize=(10,8)) sns.barplot(data=df_score,y="名前",x="社会",palette="Blues") |
ヒストグラム
1 2 3 4 5 6 7 8 |
sns.histplot(df_score, x="理科", binrange=(0,100), bins=5, hue="クラス", hue_order=["1組","2組","3組"], multiple="dodge", shrink=0.8) |
散布図
scatterplot:散布図
1 2 3 4 5 6 7 8 9 |
plt.figure(figsize=(8,8)) plt.xlim(20,100) plt.ylim(20,100) sns.scatterplot(data=df_score, x="国語", y="算数", hue="クラス", hue_order=["1組","2組","3組"], size="性別") |
lmplot:回帰直線付き散布図
1 2 3 4 5 6 |
sns.lmplot(data=df_score, x="国語", y="算数", hue="クラス", hue_order=["1組","2組","3組"], height=8) |
kdeplot:等高線風散布図
1 2 3 4 5 6 7 8 |
plt.figure(figsize=(8,8)) plt.xlim(0,150) plt.ylim(0,150) sns.kdeplot(data=df_score, x="理科", y="社会", hue="クラス", hue_order=["1組","2組","3組"]) |
jointplot:ヒストグラム付き散布図
1 2 3 4 5 6 |
sns.jointplot(data=df_score, x="国語", y="算数", hue="クラス", hue_order=["1組","2組","3組"], height=8) |
pairplot:全ペアによる散布図
1 |
sns.pairplot(data=df_score,hue="クラス") |
箱ひげ図
boxplot:箱ひげ図
1 2 |
plt.figure(figsize=(10,8)) sns.boxplot(data=df_score) |
1 2 3 4 5 6 7 |
df_score_reshape = df_score.melt(["名前","クラス","性別"],var_name="科目",value_name="点数") plt.figure(figsize=(10,8)) sns.boxplot(data=df_score_reshape, hue="クラス", hue_order=["1組","2組","3組"], x="科目", y="点数") |
violinplot:バイオリンプロット
1 2 3 4 5 6 |
plt.figure(figsize=(15,8)) sns.violinplot(data=df_score_reshape, hue="クラス", hue_order=["1組","2組","3組"], x="科目", y="点数") |
stripplot:散布図風ヒストグラム
1 2 3 4 5 6 |
plt.figure(figsize=(15,8)) sns.stripplot(data=df_score_reshape, hue="クラス", hue_order=["1組","2組","3組"], x="科目", y="点数") |
swarmplot:散布図風ヒストグラム2
1 2 3 4 5 6 |
plt.figure(figsize=(15,8)) sns.swarmplot(data=df_score_reshape, hue="クラス", hue_order=["1組","2組","3組"], x="科目", y="点数") |
ヒートマップ
heatmap:ヒートマップ
1 2 3 4 5 6 7 |
sns.set(font="Hiragino Maru Gothic Pro",context="talk") plt.figure(figsize=(8,8)) sns.heatmap(df_score.corr(), square=True, cmap="Blues", annot=True, fmt=".2f") |
clustermap:階層クラスタ付きヒートマップ
1 2 3 4 5 6 |
plt.figure(figsize=(8,8)) sns.clustermap(df_score.corr(), square=True, cmap="Blues", annot=True, fmt=".2f") |