1. 程式人生 > >18-12-11-視覺化庫Seaborn學習筆記(六:FacetGrid)

18-12-11-視覺化庫Seaborn學習筆記(六:FacetGrid)

引數:

 

data :DataFrame

整潔(“長形式”)資料框,其中每列是變數,每行是觀察。

row,col,hue:strings

定義資料子集的變數,將在網格中的不同構面上繪製。請參閱*_order引數以控制此變數的級別順序。

col_wrap:int,可選

以此寬度“包裹”列變數,以便列面跨越多行。與row小平面不相容。

share{x,y}:bool,'col'或'row'可選

如果為true,則facet將跨行和/或x軸跨行共享y軸。

height :標量,可選

每個刻面的高度(以英寸為單位)。另見:aspect

aspect:標量,可選

每個刻面的縱橫比,因此給出每個刻面的寬度,單位為英寸。aspect * height

palette :調色盤名稱,列表或字典,可選

用於hue變數的不同級別的顏色。應該是可以解釋的東西color_palette(),或者是將色調級別對映到matplotlib顏色的字典。

{row,col,hue} _order

:lists,optional

訂購分面變數的級別。預設情況下,這將是級別出現的順序,data或者,如果變數是pandas分類,則為類別順序。

hue_kws:param字典 - >值列表對映

插入到繪圖呼叫中以使其他繪圖屬性的其他關鍵字引數在色調變數的級別(例如散點圖中的標記)之間變化。

legend_out:bool,可選

如果True,圖形尺寸將被擴充套件,並且圖例將被繪製在中心右側的圖形之外。

despine:布林值,可選

從圖中移除頂部和右側脊柱。

margin_titles:bool,可選

如果True,行變數的標題被繪製到最後一列的右側。此選項是實驗性的,可能無法在所有情況下使用。

{x,y} lim:元組,可選

每個面上每個軸的限制(僅當share {x,y}為True時才相關)。

subplot_kws:dict,可選

傳遞給matplotlib subplot(s)方法的關鍵字引數字典。

gridspec_kws:dict,可選

關鍵字引數字典傳遞給matplotlib的gridspec 模組(via plt.subplots)。需要matplotlib> = 1.4,如果col_wrap不是,則忽略None

 

詳情-建議參考:Seaborn學習(一)------- 構建結構化多繪圖網格(FacetGrid()、map())詳解

1、資料準備

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import numpy as np
import pandas as pd
import seaborn as sns
from scipy import stats
import matplotlib as mpl
import matplotlib.pyplot as plt

sns.set(style="ticks")
np.random.seed(sum(map(ord, "axis_grids")))

tips = sns.load_dataset("tips")
print(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
'''
g = sns.FacetGrid(tips, col="time") # 準備畫框

g = sns.FacetGrid(tips, col="time")
g.map(plt.hist, "tip");    # plt.hist:指定為條形圖; "tip":X軸名稱

g = sns.FacetGrid(tips, col="sex", hue="smoker")
g.map(plt.scatter, "total_bill", "tip", alpha=.7) #alpha透明度
g.add_legend(); #     hue="smoker" 的分類標籤

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

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

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

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

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=.5);
g.set_axis_labels("Total bill (US Dollars)", "Tip");
g.set(xticks=[10, 30, 50], yticks=[2, 6, 10]);
g.fig.subplots_adjust(wspace=.02, hspace=.02);
#g.fig.subplots_adjust(left  = 0.125,right = 0.5,bottom = 0.1,top = 0.9, wspace=.02, hspace=.02)

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

g = sns.PairGrid(iris)
g.map_diag(plt.hist)# 對角線與非對角線
g.map_offdiag(plt.scatter);

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

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

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