5-6 可視化庫Seaborn-Facetgrid使用和繪製多變量

 

基本工做流程是FacetGrid使用數據集和用於構造網格的變量初始化對象。而後,能夠經過調用FacetGrid.map()或將一個或多個繪圖函數應用於每一個子集 FacetGrid.map_dataframe()。最後,可使用其餘方法調整繪圖,以執行更改軸標籤,使用不一樣刻度或添加圖例等操做javascript

當使用從數據集推斷語義映射的seaborn函數時,必須注意在各個方面之間同步這些映射。在大多數狀況下,使用圖形級別功能(例如relplot()或catplot())比 FacetGrid直接使用更好css

參數:

*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)html

變量 解釋 參數
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]:
 
  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.實例化對象g,並有map函數繪圖html5

In [3]:
g=sns.FacetGrid(tips,col="time")#FacetGrid當您想要在數據集的子集中分別可視化變量的分佈或多個變量之間的關係時,該類很是有用
 
 
  • 直方圖
In [4]:
g=sns.FacetGrid(tips,col="time")#先給圖佔位置
g.map(plt.hist,"tip")#做圖
Out[4]:
<seaborn.axisgrid.FacetGrid at 0xadda9b0>
 
 
  • 散點圖
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]:
<seaborn.axisgrid.FacetGrid at 0xaddafd0>
 
 
  • 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]:
<seaborn.axisgrid.FacetGrid at 0xafab0f0>
 
 
  • size大小和aspect長寬比
In [7]:
g=sns.FacetGrid(tips,col="day",size=4,aspect=0.5)
g.map(sns.barplot,"sex","total_bill")
 
E:\Software\Anaconda3_5.2.0\lib\site-packages\seaborn\axisgrid.py:703: UserWarning: Using the barplot function without specifying `order` is likely to produce an incorrect plot.
  warnings.warn(warning)
Out[7]:
<seaborn.axisgrid.FacetGrid at 0xaf51e80>
 
 
  1. 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")
 
CategoricalIndex(['Sat', 'Sun', 'Thur', 'Fri'], categories=['Thur', 'Fri', 'Sat', 'Sun'], ordered=False, dtype='category')
 
E:\Software\Anaconda3_5.2.0\lib\site-packages\seaborn\axisgrid.py:703: UserWarning: Using the boxplot function without specifying `order` is likely to produce an incorrect plot.
  warnings.warn(warning)
Out[8]:
<seaborn.axisgrid.FacetGrid at 0xb0a26a0>
 
 
  • 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]:
<seaborn.axisgrid.FacetGrid at 0xb83f588>
 
 
  • 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]:
<seaborn.axisgrid.FacetGrid at 0xbcaa0f0>
 
 
  • 軸的名字和子圖和子圖之間的間隔設置
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 指定繪圖樣式java

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 增長分類python

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 指定繪製量jquery

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 顏色繪製linux

In [16]:
g = sns.PairGrid(tips, hue = 'size', palette='GnBu_d')
g.map(plt.scatter, s = 50, edgecolor = 'white')
g.add_legend()
plt.show()
 
相關文章
相關標籤/搜索