seaborn使用版本:0.11.1 本文介绍了seaborn的使用接口,以及常用的长款表转换方法
import seaborn as sns
sns. __version__
'0.11.1'
import matplotlib. pyplot as plt
import pandas as pd
import numpy as np
Seaborn的使用结构
seaborn的借口都是扁平的,都通过seaborn.xx()调用 但是借口之间有上下级关系,存在着彼此调用 seaborn的绘图函数主要分类三类,如下所示 在每个类别中,都存在两种函数
基于坐标轴的,axes-level,简单理解就是绘制单图的函数
基于图的,Figure-level,简单理解就是绘制多图的函数,通过调用单图实现
绘图对象是FacetGrid 对应上图中的三个顶类函数relplot,displot,catplot
Figure-Level 绘图 displot
penguin = sns. load_dataset( 'penguins' , cache= True , data_home= '../../seaborn-data' )
penguin. head( )
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex 0 Adelie Torgersen 39.1 18.7 181.0 3750.0 Male 1 Adelie Torgersen 39.5 17.4 186.0 3800.0 Female 2 Adelie Torgersen 40.3 18.0 195.0 3250.0 Female 3 Adelie Torgersen NaN NaN NaN NaN NaN 4 Adelie Torgersen 36.7 19.3 193.0 3450.0 Female
displot是Figure级别的绘图函数,通过kind可以调用低级的hist,kde等绘图函数 注意与axe级别绘图不同,它的legend在图外边
sns. displot( data = penguin, x = 'flipper_length_mm' , hue = 'species' , kind= 'kde' )
<seaborn.axisgrid.FacetGrid at 0x28ab787fca0>
通过g.ax可以获得Figure-level对象的坐标轴,并添加其他子图
tips = sns. load_dataset( "tips" , cache= True , data_home= '../../seaborn-data/' )
g = sns. relplot( data= tips, x= "total_bill" , y= "tip" )
g. ax. axhline( y = 5 )
<matplotlib.lines.Line2D at 0x28abd00df10>
修改图的属性 Figure类型的图能智能将标签应用在子图上
g = sns. relplot( data= penguin, x= "flipper_length_mm" , y= "bill_length_mm" , col= "sex" )
g. set_axis_labels( "Flipper length (mm)" , "Bill length (mm)" )
<seaborn.axisgrid.FacetGrid at 0x28abd0d87c0>
使用col,row等参数,可以绘制多个子图 在参数中,可以直接加入axe函数的参数,比如bins
sns. displot( data = penguin, x = 'flipper_length_mm' , hue = 'species' , col= 'species' , cumulative = True , bins = 10 )
<seaborn.axisgrid.FacetGrid at 0x28ab74cd790>
通过height,aspect可以控制子图的大小和形状 width=height*aspect 默认情况下,每个子图都是正方形
sns. displot( data = penguin, x = 'flipper_length_mm' , hue = 'species' , col= 'species'
, cumulative = True , bins = 10 , col_wrap= 1 , height= 4 , aspect= 1.2 )
<seaborn.axisgrid.FacetGrid at 0x28ab9ff2820>
Axe-Level绘图 histplot直方图
默认会自动通过plt.gca获得当前正在绘制的axe
sns. histplot( data = penguin, x = 'flipper_length_mm' , hue = 'species' , multiple= 'stack' )
<matplotlib.axes._subplots.AxesSubplot at 0x28ab704ea30>
f, [ ax1, ax2] = plt. subplots( 1 , 2 , figsize = ( 8 , 4 ) )
sns. scatterplot( data= penguin, x= "flipper_length_mm" , y= "bill_length_mm" , hue= "species" , ax = ax1)
sns. histplot( data= penguin, x= "species" , hue= "species" , shrink= .8 , alpha= .8 , legend= False , ax= ax2)
<matplotlib.axes._subplots.AxesSubplot at 0x28aba4c55e0>
sns. kdeplot( data = penguin, x = 'flipper_length_mm' , hue = 'species' )
<matplotlib.axes._subplots.AxesSubplot at 0x28ab6ac77c0>
在同一张图上绘制多个视图
joinplot
sns. jointplot( data= penguin, x = 'flipper_length_mm' , y = 'bill_length_mm' ,
hue = 'species' )
<seaborn.axisgrid.JointGrid at 0x28ab705ce20>
pairplot()
sns. pairplot( data= penguin, hue= 'species' )
<seaborn.axisgrid.PairGrid at 0x28abc62d520>
seaborn接受的数据类型
长表long-table
flight = sns. load_dataset( 'flights' , cache= True , data_home= '../../seaborn-data/' )
flight. head( )
year month passengers 0 1949 Jan 112 1 1949 Feb 118 2 1949 Mar 132 3 1949 Apr 129 4 1949 May 121
通过指定数据源,x,y的列名来绘制图 style来指定线型
sns. relplot( data= flight, x= 'year' , y = 'passengers' , hue= 'month' , kind= 'line' , style= 'month' )
<seaborn.axisgrid.FacetGrid at 0x23dbd6a18b0>
宽表wide-table
每一个行列交汇的点是一个样本点,而变量则是index和columns seaborn不支持多级索引
flight_wide = flight. pivot( index= 'year' , columns= 'month' , values= 'passengers' )
flight_wide. head( )
month Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec year 1949 112 118 132 129 121 135 148 148 136 119 104 118 1950 115 126 141 135 125 149 170 170 158 133 114 140 1951 145 150 178 163 172 178 199 199 184 162 146 166 1952 171 180 193 181 183 218 230 242 209 191 172 194 1953 196 196 236 235 229 243 264 272 237 211 180 201
宽表绘图时,会自动将index和columns当做x,y 注意线性会不同
sns. relplot( data= flight_wide, kind= 'line' )
<seaborn.axisgrid.FacetGrid at 0x23dbd7632b0>
在使用宽表时,有时候变量会不可预测 比如当绘制单变量的图时,你不能确定index和columns哪一个被分配给x
sns. catplot( data= flight_wide, kind= "box" )
<seaborn.axisgrid.FacetGrid at 0x23dbe14b760>
脏数据
anagrams = sns. load_dataset( "anagrams" , cache= True , data_home= '../../seaborn-data/' )
anagrams
subidr attnr num1 num2 num3 0 1 divided 2 4.0 7 1 2 divided 3 4.0 5 2 3 divided 3 5.0 6 3 4 divided 5 7.0 5 4 5 divided 4 5.0 8 5 6 divided 5 5.0 6 6 7 divided 5 4.5 6 7 8 divided 5 7.0 8 8 9 divided 2 3.0 7 9 10 divided 6 5.0 6 10 11 focused 6 5.0 6 11 12 focused 8 9.0 8 12 13 focused 6 5.0 9 13 14 focused 8 8.0 7 14 15 focused 8 8.0 7 15 16 focused 6 8.0 7 16 17 focused 7 7.0 6 17 18 focused 7 8.0 6 18 19 focused 5 6.0 6 19 20 focused 6 6.0 5
ana_long = anagrams. melt( id_vars= [ "subidr" , "attnr" ] , var_name= "solutions" , value_name= "score" )
ana_long. head( )
subidr attnr solutions score 0 1 divided num1 2.0 1 2 divided num1 3.0 2 3 divided num1 3.0 3 4 divided num1 5.0 4 5 divided num1 4.0
sns. catplot( data= ana_long, x= "solutions" , y= "score" , hue= "attnr" , kind= "point" )
<seaborn.axisgrid.FacetGrid at 0x23dbce31580>