Load packages
# numerical calculation & data frames
import numpy as np
import pandas as pd
# visualization
import matplotlib.pyplot as plt
import seaborn as sns
import seaborn.objects as so
# statistics
import statsmodels.api as sm
seaborn
# numerical calculation & data frames
import numpy as np
import pandas as pd
# visualization
import matplotlib.pyplot as plt
import seaborn as sns
import seaborn.objects as so
# statistics
import statsmodels.api as sm
= sns.load_dataset("penguins")
penguins
(="body_mass_g", y="species", color="island")
so.Plot(penguins, x="sex")
.facet(col.5))
.add(so.Dot(), so.Jitter(="Set2") # color palettes: "Set2"
.scale(color=(8, 5)) # plot size
.layout(size )
= sns.load_dataset("diamonds")
diamonds
(="carat", y="price", color="carat", marker="cut")
so.Plot(diamonds, x
.add(so.Dots())
.scale(=so.Continuous("crest", norm=(0, 3), trans="sqrt"),
color
) )
(="species")
so.Plot(penguins, x
.add(so.Bar(), so.Count())=so.Nominal(order=["Adelie", "Gentoo", "Chinstrap"])) # x축의 카테고리 순서를 변경
.scale(x )
(="carat", y="price", color="carat")
so.Plot(diamonds, x
.add(so.Dots())
.scale(=so.Continuous().tick(every=0.5),
x=so.Continuous().label(like="${x:.0f}"), # %표시: like="{x:.1%}"
y=so.Continuous().tick(at=[1, 2, 3, 4]),
color
) )
Plot has a number of methods for simple customization, including Plot.label(), Plot.limit(), and Plot.share():
= sns.load_dataset("penguins")
penguins
(="body_mass_g", y="species", color="island")
so.Plot(penguins, x="sex")
.facet(col.5))
.add(so.Dot(), so.Jitter(=False)
.share(x=(2.5, -.5))
.limit(y
.label(="Body mass (g)", y="",
x=str.capitalize,
color="{} penguins".format,
title
) )
= sns.load_dataset("tips")
tips = (
p ="total_bill", y="tip", color="time")
so.Plot(tips, x
.add(so.Dot())
)"axes.facecolor": "white",
p.theme({"axes.edgecolor": "0.8",
'axes.spines.top': False,
'axes.spines.right': False})
from seaborn import axes_style
**axes_style("whitegrid")}) p.theme({
Seaborn: controlling figure aesthetics
from matplotlib import style
**style.library["fivethirtyeight"]}) p.theme({
sbcustom.py로 working directory에 저장 또는 conda 환경 폴더에 저장
"""
Seaborn.objects statistical plotting custom functions.
boxplot, rangeplot
"""
import seaborn as sns
import seaborn.objects as so
def boxplot(df, x, y, color=None, alpha=0.1, marker="<"):
return (
=x, y=y, color=color)
so.Plot(df, x=alpha, color=".6"), so.Jitter(), so.Dodge())
.add(so.Dots(alpha"median", errorbar=("pi", 50)), so.Dodge())
.add(so.Range(), so.Est(=8, marker=marker), so.Agg("median"), so.Dodge())
.add(so.Dot(pointsize="Dark2")
.scale(color**sns.axes_style("whitegrid")})
.theme({
)
def rangeplot(df, x, y, color=None, alpha=0.1):
return (
=x, y=y, color=color)
so.Plot(df, x"median", errorbar=("pi", 50)), so.Dodge())
.add(so.Range(), so.Est(=8, marker="<"), so.Agg("median"), so.Dodge())
.add(so.Dots(pointsize="Dark2")
.scale(color**sns.axes_style("whitegrid")})
.theme({ )
from sbcustom import boxplot, rangeplot
="species", y="body_mass_g") rangeplot(penguins, x
="species", y="body_mass_g", color="island") boxplot(penguins, x
Snippets: Configure User Snippets
"seaborn.obj": {
"prefix": "sbj",
"body": [
"(",
"\tso.Plot($1, x='$2', y='$3')",
"\t.add($0)",
")",
],"description": "plot seaborn.objects"
},
display()
.show()
(pyplot으로 변환; 미진한 면이 있음)
= rangeplot(penguins, x="species", y="body_mass_g")
p1 = boxplot(penguins, x="species", y="body_mass_g", color="island")
p2
# from IPython.display import display display(p1, p2)
; p2.show() p1.show()