查看数据的前五行
tips = sns.load_dataset(“tips”)
tips.head()
引入数据,布置横向画布
g = sns.FacetGrid(tips, col=’time’)
g = sns.FacetGrid(tips, col=’time’)
g.map(plt.hist, “tip”) #以tip为横轴画柱状图
g = sns.FacetGrid(tips, col=”sex”, hue=”smoker”)
g.map(plt.scatter, “total_bill”, “tip”, alpha=.7) #绘制散点图,设置横纵轴,设置透明度
g.add_legend() #加上如下图标注的图例
g = sns.FacetGrid(tips, row=”smoker”, col=”time”, margin_titles=True) #设置行列布局方式
g.map(sns.regplot, “size”, “total_bill”, color=”.1”, fit_reg=True, x_jitter=.1) #fit_reg画出回归线,x_jitter为摆动程度
画出柱形图
g = sns.FacetGrid(tips, col=”day”, size=4, aspect=.5)
g.map(sns.barplot, “sex”, “total_bill”)
from pandas import Categorical
ordered_days = tips.day.value_counts().index
print(ordered_days)
查看day的排列顺序
CategoricalIndex([‘Sat’, ‘Sun’, ‘Thur’, ‘Fri’], categories=[‘Thur’, ‘Fri’, ‘Sat’, ‘Sun’], ordered=False, dtype=’category’)
重新设置行的排列顺序
ordered_days = Categorical([“Thur”, “Sun”, “Fri”, “Sat”])
g = sns.FacetGrid(tips, row=”day”, row_order=ordered_days, size=1.7, aspect=4)
g.map(sns.boxplot, “total_bill”)