天天看點

FacertGrid()的使用

檢視資料的前五行

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")

(盒圖能夠自動識别哪個變量是離散型,哪個是連續型,然後對連續型構造盒圖。)

例如以下代碼

1 import seaborn as sns
 2 import numpy as np
 3 import pandas as pd
 4 import matplotlib as mpl
 5 from pandas import Categorical
 6 import matplotlib.pyplot as plt
 7 
 8 tips = sns.load_dataset("tips")   #seaborn内置資料集,DaraFram類型
 9 print(tips.head())
10 ordered_days = Categorical(["Thur", "Sun", "Fri", "Sat"])
11 print(type(ordered_days))
12 print(ordered_days)
13 g = sns.FacetGrid(tips, row="day", row_order=ordered_days, size=1.7, aspect=4)
14 g.map(sns.boxplot, "total_bill", "sex")
15 
16 plt.show()      

運作結果如下,函數識别出sex是離散型變量,是以對sex進行分類,然後在每一個類别上對連續型變量total_bill構造盒圖。

 還可以用FacertGrid的palette參數給hue的列的不同類設定不同顔色,代碼如下

1 import seaborn as sns
 2 import numpy as np
 3 import pandas as pd
 4 import matplotlib as mpl
 5 from pandas import Categorical
 6 import matplotlib.pyplot as plt
 7 
 8 tips = sns.load_dataset("tips")   #seaborn内置資料集,DaraFram類型
 9 print(tips.head())
10 pal = dict(Lunch="seagreen", Dinner="gray")
11 g = sns.FacetGrid(data=tips, hue="time", palette=pal, size=5)
12 g.map(plt.scatter, "total_bill", "tip", s=50, alpha=0.7, linewidths=0.5, edgecolors="white")
13 g.add_legend()
14 plt.show()      

運作結果如下

如果再設定marker參數,可指定用什麼圖示畫散點,可以是三角形或圓形等

1 import seaborn as sns
 2 import numpy as np
 3 import pandas as pd
 4 import matplotlib as mpl
 5 from pandas import Categorical
 6 import matplotlib.pyplot as plt
 7 
 8 tips = sns.load_dataset("tips")   #seaborn内置資料集,DaraFram類型
 9 print(tips.head())
10 pal = dict(Lunch="seagreen", Dinner="gray")
11 g = sns.FacetGrid(data=tips, hue="time", palette=pal, size=5, hue_kws={"marker":['^', 'v']})
12 g.map(plt.scatter, "total_bill", "tip", s=50, alpha=0.7, linewidths=0.5, edgecolors="white")
13 g.add_legend()
14 plt.show()      

還有一些小調整:set_axis_labels()函數可以自定義x和y軸名字,set(xticks, yticks)可以自定義x和y軸的刻度。fig.subplots_adjust()函數可以調整子圖之間

的間隔和距離邊框的大小。edgecolors可以設定散點周圍的邊緣顔色。

1 import seaborn as sns
 2 import numpy as np
 3 import pandas as pd
 4 import matplotlib as mpl
 5 from pandas import Categorical
 6 import matplotlib.pyplot as plt
 7 
 8 tips = sns.load_dataset("tips")   #seaborn内置資料集,DaraFram類型
 9 print(tips.head())
10 with sns.axes_style("white"):
11     g = sns.FacetGrid(tips, row="sex", col="smoker", margin_titles=True, size=2.5)
12 g.map(plt.scatter, "total_bill", "tip", color="#334488", edgecolors="white", lw=0.5)
13 g.set_axis_labels("Total_bill", "Tip")
14 g.set(xticks=[10, 30, 50], yticks=[2, 6, 10])
15 g.fig.subplots_adjust(wspace=0.25, hspace=0.25)
16 plt.show()      

可以用PairGrid對資料中的列進行兩兩配對繪制散點圖,當然也可以指定要配對的列。

1 import seaborn as sns
 2 import numpy as np
 3 import pandas as pd
 4 import matplotlib as mpl
 5 from pandas import Categorical
 6 import matplotlib.pyplot as plt
 7 
 8 iris = sns.load_dataset("iris")
 9 g = sns.PairGrid(data=iris, vars=["sepal_length", "sepal_width"], hue="species")
10 g.add_legend()
11 g.map_offdiag(plt.scatter)
12 g.map_diag(plt.hist)
13 plt.show()      

函數PairGrid()中的vars參數指定要兩兩進行繪圖的列,這些列是資料集的子列。map_offdiag和map_diag分别設定非對角的和對角的圖使用的統計圖類型。

1 import seaborn as sns
 2 import numpy as np
 3 import pandas as pd
 4 import matplotlib as mpl
 5 from pandas import Categorical
 6 import matplotlib.pyplot as plt
 7 
 8 iris = sns.load_dataset("iris")
 9 g = sns.PairGrid(data=iris, vars=["sepal_length", "sepal_width"], hue="species")
10 g.add_legend()
11 g.map_offdiag(plt.scatter)
12 g.map_diag(plt.hist)
13 plt.show()