准备数据
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
1、操作流程
- 先sns.FacetGrid画出轮廓
- 然后用map填充内容
直方图
g = sns.FacetGrid(tips, col='time')
g.map(plt.hist, "tip")
散点图
g = sns.FacetGrid(tips,col='sex',hue='smoker') # 设置参数hue,分类显示
g.map(plt.scatter,"total_bill","tip", alpha=0.7) # 参数alpha,设置点的大小
g.add_legend() # 加注释
条形图
g = sns.FacetGrid(tips,col='day',size=4,aspect=0.5)
g.map(sns.barplot,"sex","total_bill")
箱状图
from pandas import Categorical
ordered_days = tips.day.value_counts().index
print(ordered_days)
ordered_days = Categorical(['Thur',"Fri","Sat","Sun"])
# FacetGrid传数据需要是pandas格式
g = sns.FacetGrid(tips,row='day',row_order=ordered_days,size=1.7,aspect=4)
g.map(sns.boxplot,"total_bill")
网友评论