该图主要用来拟合并绘制单变量或双变量核密度估计值。
seaborn.kdeplot(data, data2=None, shade=False,
vertical=False, kernel='gau', bw='scott',
gridsize=100, cut=3, clip=None,
legend=True, cumulative=False,
shade_lowest=True, cbar=False,
cbar_ax=None, cbar_kws=None, ax=None, **kwargs)
shade:阴影:bool类型
作用:设置曲线下方是否添加阴影,如果为True则在曲线下方添加阴影
(如果数据为双变量则使用填充的轮廓绘制),若为False则,不绘制阴影
cbar:bool类型
作用:如果为True则绘制双变量KDE图,并添加颜色条
import matplotlib.pyplot as plt
import numpy as np;
import seaborn as sns;
sns.set(color_codes=True)
np.random.seed(10)
#构建数据
mean, cov = [0, 2], [(1, .5), (.5, 1)]
x, y = np.random.multivariate_normal(mean, cov, size=50).T
"""
案例1:
绘制基本的单变量密度曲线图
"""
sns.kdeplot(x)
plt.show()
import matplotlib.pyplot as plt
import numpy as np;
import seaborn as sns;
sns.set(color_codes=True)
np.random.seed(10)
#构建数据
mean, cov = [0, 2], [(1, .5), (.5, 1)]
x, y = np.random.multivariate_normal(mean, cov, size=50).T
"""
案例2:
绘制密度曲线,并在曲线下着色
通过设置color来设置不同的显示颜色
"""
sns.kdeplot(x, shade=True, color="g")
plt.show()
import matplotlib.pyplot as plt
import numpy as np;
import seaborn as sns;
sns.set(color_codes=True)
np.random.seed(10)
#构建数据
mean, cov = [0, 2], [(1, .5), (.5, 1)]
x, y = np.random.multivariate_normal(mean, cov, size=50).T
"""
案例3:
绘制双变量密度曲线
"""
sns.kdeplot(x, y)
plt.show()
import matplotlib.pyplot as plt
import numpy as np;
import seaborn as sns;
sns.set(color_codes=True)
np.random.seed(10)
#构建数据
mean, cov = [0, 2], [(1, .5), (.5, 1)]
x, y = np.random.multivariate_normal(mean, cov, size=50).T
"""
案例 4:
使用轮廓填充
"""
sns.kdeplot(x, y, shade=True,color='r')
plt.show()
import matplotlib.pyplot as plt
import numpy as np;
import seaborn as sns;
sns.set(color_codes=True)
np.random.seed(10)
#构建数据
mean, cov = [0, 2], [(1, .5), (.5, 1)]
x, y = np.random.multivariate_normal(mean, cov, size=50).T
"""
案例5:
使用更多轮廓级别和不同的调色板:
"""
sns.kdeplot(x, y, n_levels=30, cmap="Purples_d")
plt.show()
import matplotlib.pyplot as plt
import numpy as np;
import seaborn as sns;
sns.set(color_codes=True)
np.random.seed(10)
#构建数据
mean, cov = [0, 2], [(1, .5), (.5, 1)]
x, y = np.random.multivariate_normal(mean, cov, size=50).T
"""
案例6:
给轮廓添加颜色条
"""
sns.kdeplot(x, y, cbar=True)
plt.show()
import matplotlib.pyplot as plt
import numpy as np;
import seaborn as sns;
# 获取数据
iris = sns.load_dataset("iris")
setosa = iris.loc[iris.species == "setosa"]
virginica = iris.loc[iris.species == "virginica"]
"""
案例7:
绘制多个阴影双变量密度图(这里绘制两个)
"""
sns.kdeplot(setosa.sepal_width, setosa.sepal_length,cmap="Reds", shade=True, shade_lowest=False)
sns.kdeplot(virginica.sepal_width, virginica.sepal_length,cmap="Blues", shade=True, shade_lowest=False)
plt.show()
案例代码已上传:Github https://github.com/Vambooo/SeabornCN
整理制作:数据分析与可视化学研社
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。