天天看點

python可視化進階---seaborn1.10 結構化圖表可視化 FacetGrid()

結構化圖表可視化

FacetGrid()

1.基本設定

#繪制直方圖

示例1:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#設定風格、尺度
sns.set_style('ticks')
sns.set_context('paper')
import warnings 
warnings.filterwarnings('ignore')
#導入資料
tips = sns.load_dataset('tips')
print(tips.head())
#建立一個繪圖表格區域,設定好row, col并分組
g = sns.FacetGrid(tips, col = 'time', row = 'smoker')
#以total_bill 字段資料分别做直方圖統計
g.map(plt.hist, 'total_bill', alpha = 0.5, color = 'k', bins = 10)      
python可視化進階---seaborn1.10 結構化圖表可視化 FacetGrid()

示例2:

g = sns.FacetGrid(tips, col = 'day',
                  size = 4,   #圖表大小
                  aspect = 0.5  #圖表長寬比
                  )
g.map(plt.hist, 'total_bill', bins = 10,
      histtype = 'step',     #'bar', 'barstacked', 'step', 'stepfilled'
      color = 'k')      

python可視化進階---seaborn1.10 結構化圖表可視化 FacetGrid()

示例3:

g = sns.FacetGrid(tips, col = 'time', row = 'smoker')
g.map(plt.scatter,
      'total_bill','tip',   #share(x,y) ---> 設定x,y資料
      edgecolor = 'w', s = 40, linewidth = 1  #設定點大小,描邊寬度及顔色
      )
#添加圖例
g.add_legend()      

python可視化進階---seaborn1.10 結構化圖表可視化 FacetGrid()

示例4:

#建立一個繪圖表格區域,設定好col并分組,按hue分類
g = sns.FacetGrid(tips, col = 'time', hue = 'smoker')
g.map(plt.scatter,
      'total_bill','tip',   #share(x,y) ---> 設定x,y資料
      edgecolor = 'w', s = 40, linewidth = 1  #設定點大小,描邊寬度及顔色
      )
#添加圖例
g.add_legend()      

python可視化進階---seaborn1.10 結構化圖表可視化 FacetGrid()

2.圖表矩陣

#加載資料
attend = sns.load_dataset('attention')
print(attend.head())
g = sns.FacetGrid(attend, col = 'subject', 
                  col_wrap=5,#設定每行的圖表數量
                  size = 1.5
                  )
#繪制圖表矩陣
g.map(plt.plot, 'solutions', 'score',
      marker = 'o', color = 'gray', linewidth = 2)
#設定x,y軸刻度
g.set(xlim = (0,4),
      ylim = (0,10),
      xticks = [0,1,2,3,4],
      yticks = [0,2,4,6,8,10]
      )      
python可視化進階---seaborn1.10 結構化圖表可視化 FacetGrid()