seaborn.kdeplot的颜色栏 [英] Colorbar for seaborn.kdeplot

查看:158
本文介绍了seaborn.kdeplot的颜色栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Seaborn.kdeplot创建一个内核密度估计,侧面带有一个色条.

I want to create a Kernel-Density-Estimation with Seaborn.kdeplot with a colorbar on the side.

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np; np.random.seed(10)
import seaborn as sns; sns.set(color_codes=True)
mean, cov = [0, 2], [(1, .5), (.5, 1)]
x, y = np.random.multivariate_normal(mean, cov, size=50).T
sns.kdeplot(x,y,shade=True)
plt.show()

虽然创建了内核密度估计,但是我不知道如何创建颜色栏.我尝试使用plt.colorbar()没有成功.

While the Kernel-Density-Estimation is created, I do not have a clue how to create the colorbar. I tried using plt.colorbar() without success.

推荐答案

您必须直接调用scipy KDE和matplotlib轮廓函数,但这只是一些额外的代码:

You'll have to call the scipy KDE and matplotlib contour function directly, but it's just a bit of extra code:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np; np.random.seed(10)
import seaborn as sns; sns.set(color_codes=True)
from scipy import stats

mean, cov = [0, 2], [(1, .5), (.5, 1)]
data = np.random.multivariate_normal(mean, cov, size=50).T

kde = stats.gaussian_kde(data)
xx, yy = np.mgrid[-3:3:.01, -1:4:.01]
density = kde(np.c_[xx.flat, yy.flat].T).reshape(xx.shape)

f, ax = plt.subplots()
cset = ax.contourf(xx, yy, density, cmap="viridis")
f.colorbar(cset)

这篇关于seaborn.kdeplot的颜色栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆