简体   繁体   English

具有相同颜色条的多个seaborn kdeplot图

[英]multiple seaborn kdeplot plots with the same color bar

I'm trying to plot two kde distributions on the same image and I'm wondering if there is a way to use the same "color range" for both distributions. 我正在尝试在同一张图像上绘制两个kde分布,我想知道是否有一种方法可以对两个分布使用相同的“颜色范围”。

If you run the following code you'll see that the darkest red and the darkest green correspond to two different densities (0.04 and 0.15) while I would like to have both color scales with the same range so you can easily compare the two distributions (ie I would like to have the darkest red and the darkest green to correspond to the same density value). 如果运行以下代码,您会看到最暗的红色和最暗的绿色对应于两个不同的浓度(0.04和0.15),而我希望两个色标都具有相同的范围,因此您可以轻松比较这两个分布(即我想让最暗的红色和最暗的绿色对应相同的密度值)。

Thank you 谢谢

import numpy as np
import seaborn as sns
import pandas
import matplotlib.pyplot as plt
from matplotlib import rcParams

np.random.seed(10)
sns.set(color_codes=True)

rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['Arial']

plt.ioff()

f, ax = plt.subplots(figsize=(15, 15))
ax.tick_params(axis='both', which='major', labelsize=22)

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

mean, cov = [5, 7], [(3, 2), (7, 1)]
x2, y2 = np.random.multivariate_normal(mean, cov, size=50).T

ax = sns.kdeplot(x1, y1, cmap="Reds",   shade=True, shade_lowest=False, alpha=0.66, legend=False, cbar=True)
ax = sns.kdeplot(x2, y2, cmap="Greens", shade=True, shade_lowest=False, alpha=0.66, legend=False, cbar=True)

plt.xlabel("foo", fontsize=22)
plt.ylabel("bar", fontsize=22)
plt.savefig("foo_vs_bar.png")

在此处输入图片说明

Probably too late, but I just had the same problem. 可能为时已晚,但我遇到了同样的问题。 You can specify levels in kwargs, eg 您可以在kwarg中指定级别,例如

kwargs = {'levels': np.arange(0, 0.15, 0.01)}

and pass to sns.kdeplot(): 并传递到sns.kdeplot():

ax = sns.kdeplot(x1, y1, cmap="Reds",   shade=True, shade_lowest=False,
             alpha=0.66, legend=False, cbar=True, **kwargs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM