Pan*_*ter 1 python matplotlib kernel-density colorbar seaborn
我想用Seaborn.kdeplot创建一个Kernel-Density-Estimation,边上有一个颜色条.
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()
Run Code Online (Sandbox Code Playgroud)
在创建Kernel-Density-Estimation时,我不知道如何创建颜色条.我尝试使用plt.colorbar()但没有成功.
现在实施了!参数cbar=True.
您也可以使用shade_lowest=False不着色第一级。
import seaborn as sns
import numpy as np
import matplotlib.pylab as plt
x, y = np.random.randn(2, 300)
sns.kdeplot(x, y, zorder=0, n_levels=6, shade=True,
cbar=True, shade_lowest=False, cmap='viridis')
Run Code Online (Sandbox Code Playgroud)
你必须直接调用scipy KDE和matplotlib轮廓函数,但它只是一些额外的代码:
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2881 次 |
| 最近记录: |