简体   繁体   中英

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.

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

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

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

and pass to sns.kdeplot():

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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