[seaborn] seaborn学习笔记4-核密度图DENSITYPLOT

4 核密度图Densityplot

(代码下载)
核密度图显示数值变量的分布。它只需要一组数值作为输入。它非常类似于直方图。在seaborn中使用kdeplot函数绘制核密度图,该章节主要内容有:

  1. 基础核密度图绘制 Basic density plot
  2. 核密度图的区间控制 Control bandwidth of density plot
  3. 多个变量的核密度图绘制 Density plot of several variables
  4. 边际核密度图 Marginal Density plot
#调用seaborn
import seaborn as sns
#调用seaborn自带数据集
df = sns.load_dataset('iris')
#显示数据集
df.head()
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa

1. 基础核密度图绘制 Basic density plot

  • 基础核密度图函数 default density function
  • 带阴影的核密度图 Density plot with shade
  • 水平核密度图 Horizontal density plot
# 基础核密度图函数 default density function
# 纵坐标为核密度估计值,类似概率密度函数。
# 如x=3.0,纵坐标表示的不是x=3.0处的概率,而是在x=3.0附近取值的可能性估计
sns.kdeplot(df['sepal_width']);
C:\ProgramData\Anaconda3\lib\site-packages\scipy\stats\stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval

png

# 带阴影的核密度图 Density plot with shade
# density plot with shade 添加阴影线
sns.kdeplot(df['sepal_width'], shade=True);

png

# 水平核密度图 Horizontal density plot
# 基本所有seaborn绘图函数只要设置vercical就能获得水平方向的图像
sns.kdeplot(df['sepal_width'], shade=True, vertical=True, color="skyblue");

png

2. 核密度图的区间控制 Control bandwidth of density plot

# bw参数控制核密度图的区间
# 其中bw表示根据多少区间范围来计算核密度
# Large bandwidth 
sns.kdeplot(df['sepal_width'], shade=True, bw=.5, color="olive");

png

 # Narrower bandwidth
sns.kdeplot(df['sepal_width'], shade=True, bw=.05, color="olive");

png

3. 多个变量的核密度图绘制 Density plot of several variables

# 有时需要比较多个变量的核密度,可以通过matplotlib创建两个子图,也可以直接画在一张图上
p1=sns.kdeplot(df['sepal_width'], shade=True, color="r")
p1=sns.kdeplot(df['sepal_length'], shade=True, color="b")

png

4. 边际核密度图 Marginal Density plot

# 边际图能够更好变现2个数值变量之间的关系
# No space 无间隔,space表示边缘图和中央图像的间隔,kind表示图像类型
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='kde', color="grey", space=0);

png

# Huge space 大间隔
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='kde', space=3);

png

# Make marginal bigger, ratio表示中央图像和边缘图的比例,ratio越大,比例越大
sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='kde',ratio=1);

png

sns.jointplot(x=df["sepal_length"], y=df["sepal_width"], kind='kde',ratio=10);

png

  • 4
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值