Seaborn入门(一): 实现Boxplot
箱型图也是我们常用的图之一,如何用Seaborn实现箱型图呢?
与之相关的函数seaborn.boxplot的具体参数为:
seaborn.boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, dodge=True, fliersize=5, linewidth=None, whis=1.5, ax=None, **kwargs)
所提供的data是DataFrame, array, 或者 list of arrays,提供x, y也可。
hue提供分组变量。
order可以修改box的次序。
palatte给定调色板,也可以给自己的颜色列表。
orient给出box是是垂直的还是平行的,orient = 'h'是修改为平行的,'v'是垂直的。
来看几个例子:
import seaborn as sns
# sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
tips.head(5)
\ 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
ax = sns.boxplot(x=tips["total_bill"])
# 或者
ax = sns.boxplot(x="total_bill", data = tips)

对两列作图:
ax = sns.boxplot(x="day", y="total_bill", data=tips)

分组画图,并设置颜色:
ax = sns.boxplot(x="day", y="total_bill", hue="smoker",
data=tips, palette="Set3")

order参数修改box的次序:
ax = sns.boxplot(x="time", y="tip", data=tips,
order=["Dinner", "Lunch"])

平行的box:
iris = sns.load_dataset("iris")
ax = sns.boxplot(data=iris, orient="h", palette="Set2")

sns.swarmplot显示出所有的点:
ax = sns.boxplot(x="day", y="total_bill", data=tips)
ax = sns.swarmplot(x="day", y="total_bill", data=tips, color=".25")

sns.catplot分面,并修改颜色:
g = sns.catplot(x="sex", y="total_bill",
hue="smoker", col="time",
data=tips, kind="box",
palette=["#ECFC00", "#7E07A9"],
height=4, aspect=.7)

欢迎关注公众号~
