编程语言
首页 > 编程语言> > Python | Seaborn可视化合集 | 分布图(Distribution)之核密度图

Python | Seaborn可视化合集 | 分布图(Distribution)之核密度图

作者:互联网

分布图(Distribution)

3 核密度图(Density chart)

# 设置数据
import seaborn as sns
data = sns.load_dataset('iris')

# 绘制密度图
sns.kdeplot(df['sepal_width'])

OUTPUT:

3.1 添加内部阴影

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset('iris')
 
# shade添加阴影,默认为无
sns.kdeplot(data['sepal_width'], shade=True)
plt.show()

 OUTPUT:

 3.2 横向密度图

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset('iris')
 
# 设置方向
sns.kdeplot(data['sepal_width'], vertical=True)
plt.show()

OUTPUT:

 3.3 改变带宽

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset('iris')
 
# 改变带宽
sns.kdeplot(data['sepal_width'],  bw_method=0.05)
plt.show()

OUTPUT:

3.4 多变量密度图

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset('iris')
 
# 在一张图上绘制多个密度图
fig = sns.kdeplot(data['sepal_width'], color="r")
fig = sns.kdeplot(data['sepal_length'], color="b")
plt.show()

 OUTPUT:

3.5 镜像密度图

import numpy as np
from numpy import linspace
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

# 数据设置,使用DataFrame
df = pd.DataFrame({
'v1': np.random.normal(size=1000),
'v2': np.random.normal(loc=2, size=1000) * -1
})


# 绘制第一个变量密度图
sns.kdeplot(data=df, x="v1",  fill=True, alpha=1)

# 绘制第二个变量密度图
kde = gaussian_kde(df.v2)
x_range = linspace(min(df.v2), max(df.v2), len(df.v2))

# 形成镜像图
sns.lineplot(x=x_range*-1, y=kde(x_range) * -1, color='skyblue') 
plt.fill_between(x_range*-1, kde(x_range) * -1, color='skyblue')

# x轴设置        
plt.xlabel("value")
plt.axhline(y=0, linestyle='-',linewidth=1, color='black')

plt.show()

 OUTPUT:

标签:plt,kdeplot,Seaborn,Python,df,sns,import,Distribution,data
来源: https://blog.csdn.net/Luminoll/article/details/122507916