22
21

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.

distplot、displot、histplotの比較(かなり簡単)

Last updated at Posted at 2021-05-31

はじめに

ヒストグラムの描画にdistplotを使っていたが、下記のような警告文が表示される。
distplot is a deprecated function and will be removed in a future version. Please adapt your code to use either displot (a figure-level function with similar flexibility) or histplot (an axes-level function for histograms).」

将来的にdistplotがなくなる可能性があるので、distplotかhistplot使ってね、というニュアンスが書いてある。同じヒストグラムを描くにしてもどんな風に変わるか気になったので、簡単にまとめてみた。

環境(colabratory)

windows10
python:3.7.10
seaborn:0.11.1
matplotlib:3.2.2

具体例

1.単純なヒストグラムの描写

distplot
import matplotlib.pyplot as plt
import seaborn as sns;sns.set()

penguins = sns.load_dataset("penguins")
sns.distplot(penguins["flipper_length_mm"],kde=False) #カーネル密度分布なし

image.png

displot
import matplotlib.pyplot as plt
import seaborn as sns;sns.set()

penguins = sns.load_dataset("penguins")
sns.displot(penguins["flipper_length_mm"],kde=False) #カーネル密度分布なし

image.png

histplot
import matplotlib.pyplot as plt
import seaborn as sns;sns.set()

penguins = sns.load_dataset("penguins")
sns.histplot(penguins["flipper_length_mm"],kde=False)

image.png

若干描画の感じが変わっているが、コードは大きく変えなくてよさそう。

2.カーネル密度分布だけ表示

distplot
import matplotlib.pyplot as plt
import seaborn as sns;sns.set()

penguins = sns.load_dataset("penguins")
sns.distplot(penguins["flipper_length_mm"],kde=True,hist=False)

image.png

displot
import matplotlib.pyplot as plt
import seaborn as sns;sns.set()

penguins = sns.load_dataset("penguins")
sns.displot(penguins["flipper_length_mm"], kind="kde")

image.png

histplot
import matplotlib.pyplot as plt
import seaborn as sns;sns.set()

penguins = sns.load_dataset("penguins")
sns.histplot(penguins["flipper_length_mm"],alpha=0,kde=True)

image.png

こちらは1.と異なり、コードを少し変える必要が出てくる。
histplotは公式チュートリアルからヒストグラムを透明にする方法が見つけられなかったので、maplotlibで使われるalpha(透明度を0~1で指定)を使ってみた。中の色は透明になったが、枠線が見えてしまっている。histplotという名前だけあって完全に消すのは難しい?

##関数の種類
描画からdistplotとhistplotが似ていて、displotがやや違う様子だった。違いを調べてみるとどうやら階層構造が影響しているらしい。matplotlib公式HPを見るとFigure構造とAxis構造が存在し、Figure構造の中にAxis構造が入っている。
image.png

seaborn公式HPと合わせて表にまとめた。

関数名 階層構造 イメージ
distplot Axis カスタム品
displot Figure 既製品
histplot Axis カスタム品

Figure構造で設定する方が簡単だが、細かい調整(複数のグラフを同時に配置など)はAxis構造で設定する方が望ましい。

##まとめ
簡単な例だが、3つの違いを確認できた。関数のイメージはあくまでも自分の推測なので、今後検討必要(このイメージで本当に正しい?)。

※5/31 コードの間違いに気づいたので修正。

##参考
https://qiita.com/nkay/items/d1eb91e33b9d6469ef51
https://qiita.com/ceptree/items/5fb5e9e6f29d214153c9

22
21
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
22
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?