LoginSignup
34
38

More than 3 years have passed since last update.

seabornでグラフを複数のグラフを描画する

Last updated at Posted at 2019-05-04

概要

seabornで複数のグラフを複数行複数列で描画するときの設定や
y軸を揃える設定、グラフのサイズを変える設定の方法を記載します。

どうして書いたのか

seabornでグラフを複数描画するときに、
いつも「あれ、どうだったっけ?」ってなって検索するのに時間がかかるので、
他にも同じ人がいるかもと思い、記事にしました。

前提

  • 実行環境は jupyter notebook です。
  • import するのは以下。
import matplotlib.pyplot as plt
import seaborn as sns
  • 描画するデータは以下で取得できるseabornのサンプルデータを使います。
tips_df = sns.load_dataset('tips')
tips_df.head()

    total_bill  tip sex smoker  day time    size
0   16.99   1.01    Female  No  Sun Dinner  2
1   10.34   1.66    Male    No  Sun Dinner  3
2   21.01   3.50    Male    No  Sun Dinner  3
3   23.68   3.31    Male    No  Sun Dinner  2
4   24.59   3.61    Female  No  Sun Dinner  4

1行2列で描画する

subplotsに行, 列 な感じで数字を指定する。
(もし2行3列なら、 plt.subplots(2,3) となる。)

ax1とax2にsubplotsで指定した設定が入るイメージをぼくは持っています。
それを各グラフのaxに指定することで設定を反映させます。
axがパラメータとしてないグラフもあります。lmplotとか。

fig, (ax1, ax2) = plt.subplots(1, 2)

sns.regplot(x='total_bill', y='size', data=tips_df, ax=ax1)
sns.countplot(x='size', data=tips_df, ax=ax2)

2つのグラフ間でy軸を揃える

subplotsのところに sharey=True を追加する。

fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)

sns.regplot(x='total_bill', y='size', data=tips_df, ax=ax1)
sns.countplot(x='size', data=tips_df, ax=ax2)

グラフをサイズを変える

subplotsのところに figsize=(10, 20) を追加する。
()の中の数字は(横のサイズ, 縦のサイズ) です。

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10,20))

sns.regplot(x='total_bill', y='size', data=tips_df, ax=ax1)
sns.countplot(x='size', data=tips_df, ax=ax2)
34
38
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
34
38