Seaborn- 6 Facetgrid使用方法

导入所需的模块和数据集。

1 %matplotlib inline
2 import numpy as np
3 import pandas as pd
4 import matplotlib as mpl
5 import matplotlib.pyplot as plt
6 import seaborn as sns
7 sns.set(style="whitegrid", color_codes=True)
8 np.random.seed(sum(map(ord, "axis_grids")))

 

1 tips = sns.load_dataset("tips")
2 tips.head();

1 g = sns.FacetGrid(tips, col="time")
2 g.map(plt.hist, "tip"); 

1 g = sns.FacetGrid(tips, col="sex", hue="smoker")
2 g.map(plt.scatter, "total_bill", "tip", alpha=.7)
3 g.add_legend(); # 说明项

1 g = sns.FacetGrid(tips, row="smoker", col="time", margin_titles=True)
2 g.map(sns.regplot, "size", "total_bill", color=".1", fit_reg=False, x_jitter=.1);

1 g = sns.FacetGrid(tips, col="day", size=4, aspect=.5)
2 g.map(sns.barplot, "sex", "total_bill");

1 from pandas import Categorical 
2 ordered_days = tips.day.value_counts().index
3 print(ordered_days)
4 ordered_days = Categorical(['Thur', 'Fri', 'Sat', 'Sun'])
5 g = C(tips, row="day", row_order=ordered_days, size=1.7, aspect=4,)
6 g.map(sns.boxplot, "total_bill");

1 g = sns.FacetGrid(tips, hue="sex", palette="Set1", size=5, hue_kws={"marker": ["^", "v"]})
2 g.map(plt.scatter, "total_bill", "tip", s=100, linewidth=.5, edgecolor="white")
3 g.add_legend();

1 pal = dict(Lunch="seagreen", Dinner="gray")
2 g = sns.FacetGrid(tips, hue="time", palette=pal, size=5)
3 g.map(plt.scatter, "total_bill", "tip", s=50, alpha=.7, linewidth=.5, edgecolor="white")
4 g.add_legend();

1 with sns.axes_style("white"):
2     g = sns.FacetGrid(tips, row="sex", col="smoker", margin_titles=True, size=2.5)
3 g.map(plt.scatter, "total_bill", "tip", color="#334488", edgecolor="white", lw=.5);
4 g.set_axis_labels("Total bill (US Dollars)", "Tip");
5 g.set(xticks=[10, 30, 50], yticks=[2, 6, 10]); # 设置轴的取值范围
6 g.fig.subplots_adjust(wspace=.02, hspace=.02); # 设置子图之间的间隔

1 iris = sns.load_dataset("iris")
2 g = sns.PairGrid(iris)
3 g.map(plt.scatter);

1 g = sns.PairGrid(iris)
2 g.map_diag(plt.hist) # 在对角线上画什么图
3 g.map_offdiag(plt.scatter); # 在非对角线上画什么图

1 g = sns.PairGrid(iris, hue="species")
2 g.map_diag(plt.hist) 
3 g.map_offdiag(plt.scatter)
4 g.add_legend();

1 g = sns.PairGrid(iris, vars=["sepal_length", "sepal_width"], hue="species")
2 g.map(plt.scatter);

1 g = sns.PairGrid(tips, hue="size", palette="GnBu_d")
2 g.map(plt.scatter, s=50, edgecolor="white")
3 g.add_legend();

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2018-07-30 21:08  炸鸡配啤酒  阅读(255)  评论(0)    收藏  举报