0%

python数据分析与机器学习实战-17.FacetGrid使用方法及绘制多变量

1
2
3
4
5
import pandas as pd
import numpy as np
import matplotlib as mpt
import matplotlib.pyplot as plt
import seaborn as sns
1
2
tips=sns.load_dataset("tips")
tips.head()
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
1
2
g=sns.FacetGrid(tips,col="time")
<seaborn.axisgrid.FacetGrid at 0x1a1155f0f0>

png

1
2
3
g=sns.FacetGrid(tips,col="time")
g.map(plt.hist,"total_bill")
<seaborn.axisgrid.FacetGrid at 0x1a1d6a5748>

png

1
2
3
4
g = sns.FacetGrid(tips,col="sex",hue="smoker")
g.map(plt.scatter,"total_bill","tip",alpha=0.7)
g.add_legend()
<seaborn.axisgrid.FacetGrid at 0x1a1e19c0f0>

png

1
2
3
g = sns.FacetGrid(tips,col="time",row="smoker",margin_titles=True)
g.map(sns.regplot,"size","total_bill",color="0.3",fit_reg=False,x_jitter=0.1)
<seaborn.axisgrid.FacetGrid at 0x1a1eb23d68>

png

1
2
3
4
5
6
7
8
9
10
g = sns.FacetGrid(tips,col="day",size=4,aspect=0.5)
g.map(sns.barplot,"sex","total_bill")
/anaconda3/lib/python3.6/site-packages/seaborn/axisgrid.py:703: UserWarning: Using the barplot function without specifying `order` is likely to produce an incorrect plot.
warnings.warn(warning)





<seaborn.axisgrid.FacetGrid at 0x1a21180d30>

png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from pandas import Categorical
order_days = tips.day.value_counts().index
print(order_days)
order_days = Categorical(["Thur","Fri","Sat","Sun"])
g = sns.FacetGrid(tips,row="day",row_order=order_days,size=1.7,aspect=4)
g.map(sns.boxplot,"total_bill")
CategoricalIndex(['Sat', 'Sun', 'Thur', 'Fri'], categories=['Thur', 'Fri', 'Sat', 'Sun'], ordered=False, dtype='category')


/anaconda3/lib/python3.6/site-packages/seaborn/axisgrid.py:703: UserWarning: Using the boxplot function without specifying `order` is likely to produce an incorrect plot.
warnings.warn(warning)





<seaborn.axisgrid.FacetGrid at 0x1a1e4168d0>

png

1
2
3
4
5
pal = dict(Lunch="seagreen",Dinner="blue")
g = sns.FacetGrid(tips,hue="time",palette=pal,size=5)
g.map(plt.scatter,"total_bill","tip",s=50,alpha=0.7,linewidth=0.5,edgecolor="white")
g.add_legend()
<seaborn.axisgrid.FacetGrid at 0x1a2201f8d0>

png

1
2
3
4
5
6
with sns.axes_style("white"):
g=sns.FacetGrid(tips,row="sex",col="smoker",margin_titles=True,size=2.5)
g.map(plt.scatter,"total_bill","tip",color="#334488",edgecolor="white",lw=0.5)
g.set_axis_labels("total_bill(Us dollar)","tips")
g.set(xticks=[10,30,50],yticks=[2,6,10])
g.fig.subplots_adjust(wspace=0.02,hspace=0.02)

png

1
2
3
4
iris = sns.load_dataset("iris")
g = sns.PairGrid(iris)
g.map(plt.scatter)
<seaborn.axisgrid.PairGrid at 0x1a21f379e8>

png

1
2
3
4
g = sns.PairGrid(iris)
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)
<seaborn.axisgrid.PairGrid at 0x1a21a84b00>

png

1
2
3
4
5
g = sns.PairGrid(iris,hue="species")
g.map_diag(plt.hist)
g.map_offdiag(plt.scatter)
g.add_legend()
<seaborn.axisgrid.PairGrid at 0x1a23793860>

png

1
2
3
g = sns.PairGrid(iris,hue="species",vars=["sepal_length","sepal_width"])
g.map(plt.scatter)
<seaborn.axisgrid.PairGrid at 0x1a24382550>

png

1
2
3
4
g = sns.PairGrid(tips,hue="size",palette="GnBu_d")
g.map(plt.scatter,s=50,edgecolor="white")
g.add_legend()
<seaborn.axisgrid.PairGrid at 0x1a26982c88>

png

# seaborn