Colorbar for seaborn.kdeplot

烂漫一生 提交于 2019-12-13 20:03:56

问题


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()

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.


回答1:


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)



来源:https://stackoverflow.com/questions/39328551/colorbar-for-seaborn-kdeplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!