1
0

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 1 year has passed since last update.

seaborn の jointplot を patchworklib で並べてそれぞれにタイトルをつける方法

Last updated at Posted at 2023-02-24

記事の更新について

  • 2023-02-24 記事を公開
  • 2023-02-25 Patchworklib の作者の @ponnhide さんのコメントをいただき、記事を修正
    • 変更点
      • 「コピペで試せるコード例」を更新(場合を分けました)
      • set_suptitle() を使う方法の説明を追記
      • APIドキュメントへのリンクを追加

この記事について

  • seaborn の jointplot を patchworklib で並べてそれぞれにタイトルをつける方法を書いています
    • 下の図の "This is g1!"、 "This is g2!" のところがタイトル
      • image.png

コピペで試せるコード例 (各subplot自体の中央にタイトルを配置する場合)

  • こんな図ができます:
    • image.png
  • キモ
import seaborn as sns
import patchworklib as pw 
import matplotlib.pyplot as plt

sns.set_theme()
pw.overwrite_axisgrid() 

# A JointGrid
titanic = sns.load_dataset("titanic")
g1 = sns.jointplot(data=titanic, x="age", y="fare")
g1 = pw.load_seaborngrid(g1, label="g1")
g1.set_suptitle("This is g1!", fontsize = 24)

# A JointGrid
iris = sns.load_dataset("iris")
g2 = sns.jointplot(data=iris, x="sepal_width", y="petal_length",
                   kind="kde", space=0, color="g")
g2 = pw.load_seaborngrid(g2, label="g2")
g2.set_suptitle("This is g2!", fontsize = 24)

# Horizontal arrangement
(g1|g2).savefig()

コピペで試せるコード例 (subplotの中のjointplotの中の ax_marg_x の上部にタイトルを配置する場合)

  • こんな図ができます:
    • image.png
  • キモ
    • JointGrid クラスオブジェクトの ax_marg_x に対して set_title() を使用するとことで、ax_marg_x の上部にタイトルを配置します
    • 下のコードだと pw.load_seaborngrid() によって、JointGrid クラスオブジェクトだった g1 (g2) が patchworklib.Bricks クラスオブジェクトに上書きされます。そのため、pw.load_seaborngrid() を呼び出す前に、.ax_marg_x.set_title() を実行しています。
      • なので、あまり綺麗なコードではありません。
import seaborn as sns
import patchworklib as pw 
import matplotlib.pyplot as plt

sns.set_theme()
pw.overwrite_axisgrid() 

# A JointGrid
titanic = sns.load_dataset("titanic")
g1 = sns.jointplot(data=titanic, x="age", y="fare")
g1.ax_marg_x.set_title("This is g1!", fontsize = 24)
g1 = pw.load_seaborngrid(g1, label="g1")


# A JointGrid
iris = sns.load_dataset("iris")
g2 = sns.jointplot(data=iris, x="sepal_width", y="petal_length",
                   kind="kde", space=0, color="g")
g2.ax_marg_x.set_title("This is g2!", fontsize = 24)
g2 = pw.load_seaborngrid(g2, label="g2")

# Horizontal arrangement
(g1|g2).savefig()

考えたこと

[2023-02-25 追記]

なぜこういうコードにしたか

「コピペで試せるコード例 (subplotの中のjointplotの中の ax_marg_x の上部にタイトルを配置する場合)」の場合:

うまくいかなかったこと

  • suptitle を使う方法
  • patchworklib の Brick を利用する方法
    • https://zenn.dev/ponnhide/articles/40ac7dbe0aa4ab や https://qiita.com/ponnhide/items/a261defb0203561bacf1 で紹介されていた方法
    • ax1.set_title("ax1") などとしてそれぞれのサブプロットに相当するオブジェクトに対して set_title() でタイトルを設定する方法
    • seaborn の jointplotではないプロットで試した場合は動きました
    • リンク先の記事のプロットを seaborn の jointplot にした際にはうまくいきませんでした(空のプロットが表示されました)

参考

1
0
5

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?