ð Seaborn Boxplot ãã¥ãŒããªã¢ã«ïŒPython ã§ã«ã¹ã¿ã ããã¯ã¹ãããããäœæããæ¹æ³
Published on
ããã¯ã¹ããããã¯ãããŒã¿ååžã®ç解ãšããŒã¿ã®çµ±èšçãªèŠçŽãæäŸããããŒã¿ãµã€ãšã³ã¹ã®åéã§éèŠãªããŒã«ã§ããç¹ã«ãããŒã¿ãµã€ãšã³ã¹ã®ãããžã§ã¯ãã®æ¢çŽ¢çããŒã¿åæïŒEDAïŒã®ãã§ãŒãºã§åœ¹ç«ã¡ãŸãããã®ãã¥ãŒããªã¢ã«ã§ã¯ãPython ã®åŒ·åãªçµ±èšã°ã©ãã£ãã¯ã¹ããã³ããŒã¿ã®èŠèŠåããŒã«ã§ãã Seaborn ã©ã€ãã©ãªã䜿çšããŠããã¯ã¹ãããããäœæããæ¹æ³ã«çŠç¹ãåœãŠãŸããSeaborn ã®ããã¯ã¹ããããé¢æ°ãseaborn.boxplot() ã®æ§æãšã«ã¹ã¿ãã€ãºæ¹æ³ã«ã€ããŠæãäžããŸãã
Seaborn ã¯ãmatplotlib ãåºã«ãã Python ã®ããŒã¿èŠèŠåã©ã€ãã©ãªã§ããé åçã§æ å ±ãæäŸããçµ±èšçãªã°ã©ãã£ãã¯ã¹ãæãããã®ã·ã³ãã«ãªã€ã³ã¿ãŒãã§ã€ã¹ãæäŸããŸããSeaborn ã®äž»ãªç¹åŸŽã®1ã€ã¯ãããŒã¿ã® 5 çªç®ã®èŠçŽã®ã°ã©ãã£ã«ã«ãªè¡šçŸã§ããããã¯ã¹ãããããäœæã§ããããšã§ããSeaborn ã®ããã¯ã¹ããããé¢æ°ãseaborn.boxplot() ã¯ããããã®ãããããç°¡åã«æè»ã«äœæã§ãããã¯ãã«ãªããŒã«ã§ãã
Python ãã³ãã¹ããŒã¿ãã¬ãŒã ããã³ãŒããªãã§ããŒã¿èŠèŠåãè¿ éã«äœæãããã§ããïŒ
PyGWalker ã¯ãããŒã¿èŠèŠåã«ããã Python ã©ã€ãã©ãªã§ãã PyGWalker (opens in a new tab) ã¯ãJupyter Notebook ã®ããŒã¿è§£æãšããŒã¿èŠèŠåã¯ãŒã¯ãããŒãç°¡çŽ åããããšãã§ããŸãããã³ãã¹ããŒã¿ãã¬ãŒã ïŒããã³ããŒã©ããŒã¿ãã¬ãŒã ïŒã Tableau ã¹ã¿ã€ã«ã®ãŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ã«å€æããŠèŠèŠçãªæ¢çŽ¢ãå¯èœã§ãã
Seaborn ããã¯ã¹ãããããšã¯ãã©ã®ããã«äœ¿çšãããã®ãïŒ
Seaborn ã®ããã¯ã¹ããããã¯ãååäœæ°ãéããŠæ°å€ããŒã¿ã®ã°ã«ãŒããã°ã©ãã£ã«ã«ã«è¡šçŸããæ¹æ³ã§ããããã¯ã¹ããããã¯ããŒã¿ã®èŠèŠçãªèŠçŽãæäŸããããã¯ã¹ã¯ååäœç¯å²ïŒããŒã¿ã®äžå€® 50%ïŒãè¡šããããã¯ã¹å ã®ç·ã¯äžå€®å€ãè¡šããŸãããŸããã²ãã¯ããŒã¿ã®ç¯å²ã瀺ããŸããå€ãå€ãããå Žåã¯ãã²ãã®å€åŽã«åã ã®ç¹ãšããŠè¡šç€ºãããŸãã
ããã¯ã¹ããããã¯ãç°ãªãããŒã¿ã»ããéã®ååžãæ¯èŒããããã«äœ¿çšãããŸããããšãã°ãç°ãªãæ宀éã®ãã¹ãã¹ã³ã¢ã®ååžãç°ãªãæã®æ°æž©ã®ååžãæ¯èŒãããå ŽåããããŸããSeaborn ã®ããã¯ã¹ããããé¢æ°ã䜿çšãããšããããã®ãããããç°¡åã«äœæããå¿ èŠã«å¿ããŠã«ã¹ã¿ãã€ãºããããšãã§ããŸãã
Python 㧠Seaborn ããã¯ã¹ãããããäœæãã
Seaborn ã®ããã¯ã¹ãããããäœæããã«ã¯ããŸã Seaborn ã©ã€ãã©ãªãã€ã³ããŒãããå¿ èŠããããŸãã次ã®ã³ãŒãè¡ã§ãããè¡ãããšãã§ããŸãã
import seaborn as sns
次ã«ãããŒã¿ãèªã¿èŸŒãå¿ èŠããããŸããSeaborn 㯠Pandas ããŒã¿ãã¬ãŒã ãæ±ãããšãã§ãããããããŒã¿ãããŒã¿ãã¬ãŒã ã«èªã¿èŸŒãã§ãããSeaborn ã®ããã¯ã¹ããããé¢æ°ã«æž¡ãããšãã§ããŸãã以äžã«äŸã瀺ããŸãã
## ããã©ã«ãã® tips ããŒã¿ã»ãããèªã¿èŸŒã
tips = sns.load_dataset("tips")
## ããã¯ã¹ãããããäœæãã
sns.boxplot(x=tips["total_bill"])
ãã®äŸã§ã¯ãSeaborn ã®çµã¿èŸŒã¿ tips ããŒã¿ã»ãããèªã¿èŸŒã¿ãåèšè«æ±éé¡ã®ããã¯ã¹ãããããäœæããŠããŸãã
Seaborn ããã¯ã¹ããããã®å€èŠ³ãã«ã¹ã¿ãã€ãºãã
Seaborn ã®ããã¯ã¹ããããã¯ãå€èŠ³ãå€æ§ã«ã«ã¹ã¿ãã€ãºããŠãããæ å ±ãæäŸããèŠæ ããåäžãããããšãã§ããŸããSeaborn ããã¯ã¹ãããããã«ã¹ã¿ãã€ãºããããã€ãã®äŸã玹ä»ããŸãã
æ¹åã®å€æŽ
ããã©ã«ãã§ã¯ãSeaborn ã¯åçŽãªããã¯ã¹ãããããäœæããŸãããã ããx ãã©ã¡ãŒã¿ãš y ãã©ã¡ãŒã¿ãåãæ¿ããããšã§æ°Žå¹³ãªããã¯ã¹ãããããäœæããããšãã§ããŸãã以äžã«äŸã瀺ããŸãã
## æ°Žå¹³ãªããã¯ã¹ãããããäœæãã
sns.boxplot(y=tips["total_bill"])
Hue ã®è¿œå
ããã¯ã¹ããããã« hue ãã©ã¡ãŒã¿ãè¿œå ãããšãå¥ã®ã«ããŽãªå€æ°ã§ããã¯ã¹ãåå²ããããšãã§ããŸããããã«ãããç°ãªãã°ã«ãŒãéã®ååžãæ¯èŒããã®ã«åœ¹ç«ã¡ãŸãã以äžã«äŸã瀺ããŸãã
## Hue ãæå®ããããã¯ã¹ãããããäœæãã
sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips)
ãã®äŸã§ã¯ãææ¥ããšã«é¡§å®¢ãå«ç è ãã©ããã«ãã£ãŠåèšè«æ±éé¡ãåå²ããããã¯ã¹ãããããäœæããŠããŸãã
ããã¯ã¹ã®è²ã®ã«ã¹ã¿ãã€ãº
Seaborn ã§ã¯ãããã¯ã¹ããããã®è²ãã«ã¹ã¿ãã€ãºããããšãã§ããŸããããã¯ãboxplot é¢æ°ã® palette ãã©ã¡ãŒã¿ã«ã«ã©ãŒãã¬ãããæž¡ãããšã§å®çŸã§ããŸãã以äžã«äŸã瀺ããŸãã
## ã«ã¹ã¿ã ã«ã©ãŒã§ããã¯ã¹ãããããäœæãã
sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips, palette="Set3")
ãã®äŸã§ã¯ã"Set3" ã«ã©ãŒãã¬ããã䜿çšããŠããã¯ã¹ããããã®è²ãèšå®ããŠããŸãã
ã²ãã®å€æŽ
ããã©ã«ãã§ã¯ãSeaborn ã®ããã¯ã¹ããããã®ã²ãã¯å€ãå€ãé€ããããŒã¿ã®ç¯å²ãè¡šããŸãããã ããboxplot é¢æ°ã® whis ãã©ã¡ãŒã¿ã«ç°ãªãå€ãæž¡ãããšã§ããããå€æŽããããšãã§ããŸããããšãã°ãwhis ã 0.5 ã«èšå®ãããšãã²ãã¯ããŒã¿ã®ç¬¬ 5 ããŒã»ã³ã¿ã€ã«ãšç¬¬ 95 ããŒã»ã³ã¿ã€ã«ãè¡šããŸãã以äžã«äŸã瀺ããŸãã
## ã«ã¹ã¿ã ã²ãã§ããã¯ã¹ãããããäœæãã
sns.boxplot(x="day", y="total_bill", data=tips, whis=0.5)
Seaborn ããã¯ã¹ãããã vs ãã€ãªãªã³ãããã
Seaborn ã¯ãããã¯ã¹ããããã«ã«ãŒãã«å¯åºŠæšå®ãçµã¿åããããã€ãªãªã³ãããããšããå¥ã®ããããã¿ã€ããæäŸããŠããŸãããã€ãªãªã³ããããã¯ããŒã¿ã®ååžã®ãã詳现ãªæ å ±ãæäŸãããããããã¯ã¹ãããããããè±ããªèšè¿°ãæäŸããŸãã
ãã ãããã€ãªãªã³ããããã¯è§£éãããè€éã§ããããã¹ãŠã®èŠ³å®¢ã«é©ããŠããããã§ã¯ãããŸãããäžæ¹ãããã¯ã¹ããããã¯ããããããåºãç解ãããŠãããå€ãã®ç¶æ³ã«é©ããéžæè¢ã§ãã
以äžã«ãSeaborn ã§ãã€ãªãªã³ãããããäœæããäŸã瀺ããŸãã
## ãã€ãªãªã³ãããããäœæãã
sns.violinplot(x="day", y="total_bill", data=tips)
Seabornããã¯ã¹ããããã®è§£é
seabornã®ããã¯ã¹ããããã解éããã«ã¯ãããããã®ç°ãªãã³ã³ããŒãã³ããç解ããå¿ èŠããããŸããçãäžã®ç®±ã¯ååäœç¯å²ïŒIQRïŒãè¡šããããã¯ç¬¬1ååäœïŒ25çªç®ã®ããŒã»ã³ã¿ã€ã«ïŒãšç¬¬3ååäœïŒ75çªç®ã®ããŒã»ã³ã¿ã€ã«ïŒã®éã®ç¯å²ã§ããç®±ã®å åŽã®ç·ã¯äžå€®å€ããŸãã¯ããŒã¿ã®50çªç®ã®ããŒã»ã³ã¿ã€ã«ã§ããã²ãã¯ã1.5åã®IQRå ã®ããŒã¿ã®ç¯å²ãè¡šããŠããŸãããã®ç¯å²å€ã®ããŒã¿ãã€ã³ãã¯å€ãå€ãšããŠæ±ãããåå¥ã®ãã€ã³ããšããŠè¡šãããŸãã
次ã¯ãseabornããã¯ã¹ããããã®è§£éæ¹æ³ã®äŸã§ãïŒ
## ããã¯ã¹ãããããäœæãã
sns.boxplot(x="day", y="total_bill", data=tips)
ãã®ããã¯ã¹ããããã§ã¯ãåæ¥ã®äžå€®å€ã®åèšè«æ±é¡ããåç®±ã®å åŽã®ç·ã§è¡šãããŸããç®±ã¯IQRãè¡šããŠããã®ã§ãåæ¥ã®é¡§å®¢ã®äžå€®50ïŒ ã®åèšè«æ±é¡ã®ç¯å²ã確èªã§ããŸããã²ãã¯ã1.5åã®IQRå ã®åèšè«æ±é¡ã®ç¯å²ã瀺ãããã®ç¯å²å€ã®ãã€ã³ãã¯å€ãå€ã§ãã
Seabornããã¯ã¹ããããã®æ³šé
Seabornããã¯ã¹ããããã«ã¯ãè¿œå æ å ±ãæäŸããããã«æ³šéãè¿œå ã§ããŸããããšãã°ãäžå€®å€ãååäœæ°ããŸãã¯å€ãå€ã«å€ã泚éä»ããããšãã§ããŸãã次ã¯ãseabornããã¯ã¹ããããã«æ³šéãè¿œå ããæ¹æ³ã®äŸã§ãïŒ
## ããã¯ã¹ãããããäœæãã
ax = sns.boxplot(x="day", y="total_bill", data=tips)
## 泚éãè¿œå ãã
for patch in ax.artists:
r, g, b, a = patch.get_facecolor()
patch.set_facecolor((r, g, b, .3))
x = patch.get_x()
y = patch.get_y()
width = patch.get_width()
height = patch.get_height()
ax.text(x+width/2, y+height/2, "{:.2f}".format(height), ha='center', va='center')
plt.show()
ãã®äŸã§ã¯ãããã¯ã¹ããããã«æ³šéãè¿œå ããŠãåç®±ã®é«ãã衚瀺ããŠããŸããããã¯IQRãè¡šããŠããŸãã
çµè«
çµè«ãšããŠãseabornã®ããã¯ã¹ããããã¯ãããŒã¿ã®ååžãèŠèŠåãç解ããããã®åŒ·åãªããŒã«ã§ããseabornã©ã€ãã©ãªã䜿çšãããšããããæ°è¡ã®ã³ãŒãã§é åçã§æ å ±éã®å€ãããã¯ã¹ãããããäœæããããšãã§ããŸããæ°ããããŒã¿ã»ãããæ¢çŽ¢ããããã¬ããŒããæºåãããããéã«ãseabornã®ããã¯ã¹ããããã¯å¿ èŠãªæŽå¯ãåŸãã®ã«åœ¹ç«ã¡ãŸãã
FAQ
Seabornããã¯ã¹ãããããšã¯äœã§ããïŒã©ã®ããã«äœ¿çšãããŸããïŒ
Seabornããã¯ã¹ããããã¯ããŒã¿ã»ããã®ååžãã°ã©ãã£ã«ã«ã«è¡šãããã®ã§ãããŒã¿ã®äžå€®å€ãååäœæ°ãå€ãå€ã衚瀺ããŸããããŒã¿ã®ååžãèŠèŠåãç解ããããã«äœ¿çšãããç°ãªãããŒã¿ã°ã«ãŒãéã®ååžãæ¯èŒããããã«ã䜿çšãããŸãã
Pythonã§Seabornããã¯ã¹ãããããäœæããã«ã¯ã©ãããã°ããã§ããïŒ
Pythonã§Seabornããã¯ã¹ãããããäœæããã«ã¯ãseaborn.boxplot()é¢æ°ã䜿çšããŸãããã®é¢æ°ã«ããŒã¿ãæž¡ãå¿ èŠããããããŸããŸãªãã©ã¡ãŒã¿ã䜿çšããŠããã¯ã¹ããããã®èŠãç®ãã«ã¹ã¿ãã€ãºããããšãã§ããŸãã
Seabornããã¯ã¹ããããã®èŠãç®ãã«ã¹ã¿ãã€ãºããæ¹æ³ã¯ãããŸããïŒ
Seabornããã¯ã¹ããããã®èŠãç®ãããŸããŸãªæ¹æ³ã§ã«ã¹ã¿ãã€ãºããããšãã§ããŸããæ¹åã®å€æŽãhueã®è¿œå ãè²ã®ã«ã¹ã¿ãã€ãºãã²ãã®å€æŽãªã©ããããŸãããŸããè¿œå æ å ±ãæäŸããããã«æ³šéãè¿œå ããããšãã§ããŸãã