以下操作都是翻译的官方文档(不全)
安装seaborn

pip3 install seaborn

seborn加载数据集

import seaborn as sb 
df = sb.load_dataset('tips') 
print(type(df))
print(df.head)

可以看出df的类型是<class 'pandas.core.frame.DataFrame'>,所以在使用seaborn之前需要安装pandas

查看支持多少数据集

import seaborn as sb 
print(sb.get_dataset_names())

不安装bs4会报错

pip3 install bs4

seaborn绘制各种图

matplotlib画图

import numpy as np 
from matplotlib import pyplot as plt 
def sinplot(flip=1): 
    x = np.linspace(0, 14, 100) 
    for i in range(1, 5): 
        plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)         
sinplot() 
plt.show()

seaborn绘制各种图

sb.set()

转化为seaborn的默认格式

import numpy as np 
from matplotlib import pyplot as plt 
def sinplot(flip=1): 
    x = np.linspace(0, 14, 100) 
    for i in range(1, 5): 
        plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)   
 
import seaborn as sb 
sb.set() 
sinplot() 
plt.show()

seaborn绘制各种图

set_style()
设置画图格式,可供选择的如下

  • white
  • dark
  • whitegrid
  • darkgrid
  • ticks

如下

import numpy as np 
from matplotlib import pyplot as plt 
def sinplot(flip=1): 
    x = np.linspace(0, 14, 100) 
    for i in range(1, 5): 
        plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)   
 
import seaborn as sb 
sb.set_style('whitegrid')
sinplot() 
plt.show()

seaborn绘制各种图

sb.despine()
在white 和 ticks 主题下,可以通过这个函数去掉上部和右侧的图像边框线

自定义样式

sb.set_style()

查看格式中有多少元素,比入线的粗细等等
seaborn绘制各种图
一个demo

sb.set_style("darkgrid", {'axes.axisbelow': False}) 

直方图

import pandas as pd 
import seaborn as sb 
from matplotlib import pyplot as plt 
df = sb.load_dataset('iris') #鸢尾花数据集
sb.distplot(df['petal_length'],kde=False) 
plt.show()

其中kde设置为False代表只画直方图,设置为True代表除了直方图还有折线图

seaborn绘制各种图
seaborn绘制各种图

散点图

import pandas as pd 
import seaborn as sb 
from matplotlib import pyplot as plt 
df = sb.load_dataset('iris') 
sb.jointplot(x='petal_length',y='petal_width',data=df) 
plt.show()

seaborn绘制各种图

六边形二元化方法

在数据密度稀疏的情况下,二元数据分析采用六边形二元化方法,当数据非常零散且难以通过散点图进行分析时可以用这种方法。

import pandas as pd 
import seaborn as sb 
from matplotlib import pyplot as plt 
df = sb.load_dataset('iris') 
sb.jointplot(x='petal_length',y='petal_width',data=df,kind='hex') 
plt.show()

seaborn绘制各种图

核密度估计

核密度估计是一种估计变量分布的非参数方法。在seaborn中,我们可以使用jointplot()
kind参数使用kde

import pandas as pd 
import seaborn as sb 
from matplotlib import pyplot as plt 
df = sb.load_dataset('iris') 
sb.jointplot(x='petal_length',y='petal_width',data=df,kind='kde') 
plt.show() v

seaborn绘制各种图

核密度估计

import pandas as pd 
import seaborn as sb 
from matplotlib import pyplot as plt 
df = sb.load_dataset('iris') 
sb.distplot(df['petal_length'],hist=False) 
plt.show() 

hist设置为false即生成核密度估计图

seaborn绘制各种图

可视化成对关系

seaborn.pairplot(data,…)

参数介绍

parameter description
data Datafram
hue Variable in data to map plot aspects to different colors.
palette Set of colors for mapping the hue variable
kind Kind of plot for the non-identity relationships. {‘scatter’, ‘reg’}
diag_kind Kind of plot for the diagonal subplots. {‘hist’, ‘kde’}

直接复制的官方文档的介绍

import pandas as pd
import seaborn as sb 
from matplotlib import pyplot as plt 
df = sb.load_dataset('iris') 
sb.set_style("ticks") 
sb.pairplot(df,hue='species',diag_kind="kde",kind="scatter",palette="husl") 
plt.show()

seaborn绘制各种图

绘制分类数据

import pandas as pd 
import seaborn as sb 
from matplotlib import pyplot as plt 
df = sb.load_dataset('iris') 
sb.stripplot(x="species", y="petal_length", data=df) 
plt.show()

seaborn绘制各种图

Swarmplot()

另外一种风格

import pandas as pd 
import seaborn as sb 
from matplotlib import pyplot as plt 
df = sb.load_dataset('iris') 
sb.swarmplot(x="species", y="petal_length", data=df) 
plt.show()

seaborn绘制各种图

柱状图

import pandas as pd 
import seaborn as sb 
from matplotlib import pyplot as plt 
df = sb.load_dataset('titanic') 
sb.barplot(x="sex", y="survived", hue="class", data=df) 
plt.show() 

seaborn绘制各种图

省略了几种柱状图

线性关系

import pandas as pd 
import seaborn as sb 
from matplotlib import pyplot as plt 
df = sb.load_dataset('tips') 
sb.regplot(x="total_bill", y="tip", data=df) 
sb.lmplot(x="total_bill", y="tip", data=df) 
plt.show()

regplot vs lmplot

regplot lmplot
接受各种格式的x和y变量,包括简单的numpy数组、pandas系列对象,或作为pandas数据帧中变量的引用 将数据作为必需参数,并且必须将x和y变量指定为字符串。这种数据格式称为“长格式”数据

seaborn绘制各种图

当其中一个变量取离散值时,我们也可以拟合线性回归

import pandas as pd 
import seaborn as sb 
from matplotlib import pyplot as plt 
df = sb.load_dataset('tips') 
sb.lmplot(x="size", y="tip", data=df) 
plt.show()

seaborn绘制各种图

热力图

import numpy as np; np.random.seed(0)
import seaborn as sb; 
sb.set()
uniform_data = np.random.rand(10, 12)
ax = sb.heatmap(uniform_data)

seaborn绘制各种图

玩了一下powerbi嵌入代码

相关文章: