结构化图表可视化 - FacetGrid()
import numpy as np
import pandas as pd
import matplotlib. pyplot as plt
import seaborn as sns
% matplotlib inline
sns. set_style( "ticks" )
sns. set_context( "paper" )
import warnings
warnings. filterwarnings( 'ignore' )
1、基本设置
绘制直方图1
tips = sns. load_dataset( 'tips' )
g = sns. FacetGrid( tips, col= 'time' , row= 'smoker' )
g. map ( plt. hist, 'total_bill' , alpha= 0.5 , color= 'b' , bins= 10 )
绘制直方图2
g = sns. FacetGrid( tips, col= 'day' ,
size= 6 ,
aspect= 0.5
)
g. map ( plt. hist, 'total_bill' , bins= 10 ,
histtype= 'step' ,
color= 'r' )
绘制散点图
g = sns. FacetGrid( tips, col= 'time' , row= 'smoker' )
g. map ( plt. scatter, 'total_bill' , 'tip' ,
edgecolor= 'w' , s= 40 , linewidth= 1
)
g. add_legend( )
基本设置 - 分类
g = sns. FacetGrid( tips, col= 'time' , hue= 'smoker' )
g. map ( plt. scatter,
'total_bill' , 'tip' ,
edgecolor= 'w' , s= 40 , linewidth= 1 )
g. add_legend( )
2、图表矩阵
attend = sns. load_dataset( 'attention' )
g = sns. FacetGrid( attend, col= 'subject' , col_wrap= 5 ,
size= 2.5
)
g. map ( plt. plot, 'solutions' , 'score' ,
marker= 'o' , color= 'g' , linewidth= 2 )
g. set ( xlim= ( 0 , 4 ) ,
ylim= ( 0 , 10 ) ,
xticks= [ 0 , 1 , 2 , 3 , 4 ] ,
yticks= [ 0 , 2 , 4 , 6 , 8 , 10 ] )