基本工作流程是FacetGrid使用數據集和用於構造網格的變量初始化對象。然后,可以通過調用FacetGrid.map()或將一個或多個繪圖函數應用於每個子集 FacetGrid.map_dataframe()。最后,可以使用其他方法調整繪圖,以執行更改軸標簽,使用不同刻度或添加圖例等操作
當使用從數據集推斷語義映射的seaborn函數時,必須注意在各個方面之間同步這些映射。在大多數情況下,使用圖形級別功能(例如relplot()或catplot())比 FacetGrid直接使用更好
參數:¶
*class seaborn.FacetGrid(data, row=None, col=None, hue=None, col_wrap=None, sharex=True, sharey=True, height=3, aspect=1, palette=None, row_order=None, col_order=None, hue_order=None, hue_kws=None, dropna=True, legend_out=True, despine=True, margin_titles=False, xlim=None, ylim=None, subplot_kws=None, gridspec_kws=None, size=None)
變量 | 解釋 | 參數 |
---|---|---|
data | 處理后的(“長格式”)dataframe數據,其中每一列都是一個變量(特征),每一行都是一個樣本 | DataFrame |
row, col, hue | 定義數據子集的變量,這些變量將在網格的不同方面繪制。請參閱下面*_order參數以控制該變量的級別順序 | strings |
col_wrap | 這個意思是圖網格列維度限制 | int, optional |
share{x,y} | 是否共享x軸或者y軸,就是說如果為真,就共享同一個軸,否則就不共享,默認是都共享,即都為True | bool, ‘col’, or ‘row’ optional |
height | 每個圖片的高度設定,默認為3 | scalar, optional |
aspect | 文檔說是縱橫比,是說每個小圖的橫軸長度和縱軸的比 | scalar, optional |
palette | 這個簡單,一般在使用hue時來改變顏色的,有這幾種系統給的可選deep, muted, bright, pastel, dark, colorblind | palette name, list, or dict, optional |
{row,col,hue}_order | 對所給命令的級別進行排序。默認情況下,這將是數據中顯示的級別,如果變量是pandas分類,則是類別順序。 | lists, optional |
hue_kws | 其他關鍵字參數插入到繪圖調用中,讓其他繪圖屬性在色相變量的級別上有所不同(例如散點圖中的標記)hue_kws=dict(marker=["^", "v"])) # 給顏色語意使用不同的標簽, | dictionary of param -> list of values mapping |
legend_out | 默認為True,legend是圖例的意思,如果True,圖形尺寸將被擴展,並且圖例將被繪制在中心右側的圖形之外,為False時,圖例單獨放出來 | bool, optional |
despine | 從圖中移除頂部和右側脊柱,就是邊緣框架 | boolean, optional |
margin_titles | 如果是真的,那么行變量的標題就會被繪制到最后一列的右邊。這個選項是實驗性的,在所有情況下都可能不起作用。 | bool, optional |
{x, y}lim | 每個方面的每個軸的限制(只有當共享x時才相關,y是正確的 | tuples, optional |
subplot_kws | 傳遞給matplotlib的subplot(s) 方法的關鍵字參數字典Dictionary of keyword arguments passed to matplotlib subplot(s) methods.這個需要看看subplot函數的參數,后面有時間補上 | dict, optional |
gridspec_kws | 傳遞給matplotlib的gridspec模塊的關鍵字參數的字典(Via plt.subplots)。matplotlib >= 1.4,如果colwrap不是None,則會被忽略 | dict, optional |
In [1]:
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats,integrate
import matplotlib.pyplot as plt
import seaborn as sns
np.random.seed(sum(map(ord,"axis_grids")))
In [2]:
tips=sns.load_dataset("tips")
tips.head()
Out[2]:
1.實例化對象g,並有map函數繪圖
In [3]:
g=sns.FacetGrid(tips,col="time")#FacetGrid當您想要在數據集的子集中分別可視化變量的分布或多個變量之間的關系時,該類非常有用
- 直方圖
In [4]:
g=sns.FacetGrid(tips,col="time")#先給圖占位置
g.map(plt.hist,"tip")#作圖
Out[4]:
- 散點圖
In [5]:
g=sns.FacetGrid(tips,col="sex",hue="smoker")#先給圖占位置
g.map(plt.scatter,"total_bill","tip",alpha=0.7)#作圖,定義透明程度
g.add_legend()
Out[5]:
- row,col定義畫出多個子圖
- regplot繪制回歸圖,定義fit_reg是否繪制回歸線條,x_jitter或y_jitter定義指定軸的浮動系數
In [6]:
g=sns.FacetGrid(tips,row="smoker",col="time",margin_titles=True)#margin_titles=True,顯示邊上的title
g.map(sns.regplot,"size","total_bill",color=".1",fit_reg=False,x_jitter=.1)#fit_reg=False,x_jitter=.1。分別是指擬合回歸大小指定系數
Out[6]:
- size大小和aspect長寬比
In [7]:
g=sns.FacetGrid(tips,col="day",size=4,aspect=0.5)
g.map(sns.barplot,"sex","total_bill")
Out[7]:
- pandas.Categorical(values, categories=None, ordered=None, dtype=None, fastpath=False)#對數據分類,Categorical是一種數據類型, ordered可以定義順序
In [8]:
from pandas import Categorical
ordered_days=tips.day.value_counts().index
print(ordered_days)
#order_days=Categorical(['Thur','Fri','Sat',Sun']),是指定順序
g=sns.FacetGrid(tips,row="day",row_order=ordered_days,
size=1.7,aspect=4)
g.map(sns.boxplot,"total_bill")
Out[8]:
- FacetGrid的 palette指定類別的顏色
In [9]:
pal=dict(Lunch="seagreen",Dinner="gray")#指定,palette顏色
#palette參數,一般在使用hue時來改變顏色的,有這幾種系統給的可選deep, muted, bright, pastel, dark, colorblind
g=sns.FacetGrid(tips,hue="time",palette=pal,size=5)
g.map(plt.scatter,"total_bill","tip",s=50,alpha=0.6,linewidth=0.5,edgecolor="white")#s=>size,edgecolor=>顏色
g.add_legend()
Out[9]:
- FacetGrid的 marker的形狀及坐標范圍指定
In [10]:
g = sns.FacetGrid(tips, hue='sex', palette='Set1', size=5, hue_kws={"marker":["^","v"]})
#g = sns.FacetGrid(tips, hue='sex',palette='Set1',size = 5, hue_kws={"marker":["^","v"]})
g.map(plt.scatter,"total_bill","tip",s=100,linewidth=0.5,edgecolor="white")
g.add_legend()#馬如蛟,15185176184,機械工程學院
Out[10]:
- 軸的名字和子圖和子圖之間的間隔設置
In [11]:
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 Dollars)",'Tip')#軸的名字
g.set(xticks = [10,30,50], yticks =[2,6,10])#x,y軸的刻度
g.fig.subplots_adjust(wspace=.02,hspace=.02)#子圖和子圖之間的間隔設置
#g.fig.subplots_adjust():left,right,top,bottom,wspace,hspace可以設置這幾個位置的間距
plt.show()
PairGrid繪圖¶
In [12]:
iris = sns.load_dataset('iris')
g = sns.PairGrid(iris)
g.map(plt.scatter,s = 20, edgecolor = 'black')
plt.show()
3-1 指定繪圖樣式
In [13]:
g = sns.PairGrid(iris)
g.map_diag(plt.hist,edgecolor = 'black')#對角線上圖片
g.map_offdiag(plt.scatter,s = 20, edgecolor = 'black')#非對角線上圖片
plt.show()
3-2 增加分類
In [14]:
g = sns.PairGrid(iris,hue= 'species')
g.map_diag(plt.hist,edgecolor = 'black')#對角線上圖片
g.map_offdiag(plt.scatter,s = 20, edgecolor = 'black')#非對角線上圖片
g.add_legend()
plt.show()
3-3 指定繪制量
In [15]:
g = sns.PairGrid(iris, vars=['sepal_length','sepal_width'],hue = 'species')
g.map(plt.scatter,s = 20, edgecolor = 'black')
plt.show()
3-4 顏色繪制
In [16]:
g = sns.PairGrid(tips, hue = 'size', palette='GnBu_d')
g.map(plt.scatter, s = 50, edgecolor = 'white')
g.add_legend()
plt.show()