使用seaborn的kdeplots绘制多个填充轮廓子图时的空白

问题描述 投票:0回答:2

我编写了一个 python 函数,用于绘制一系列填充轮廓,该函数使用

kdeplot
seaborn
函数从由 numpy 数组列表组成的数据集的某些列中获取数据。代码如下:

import matplotlib.pyplot as plt
import seaborn as sns

def kdeplots_figure(dataset):
    cols = int((len(dataset)-2)/2)
    figure, axes = plt.subplots(1, cols, figsize=(15,6), sharey=True)
    f = 2
    for i in range (len(axes)):
        sns.set_style("white")
        axes[i].set_title(" Cluster {0}".format(i), fontsize = 16)
        axes[i].set_ylabel('Y Axis', fontsize = 14)
        axes[i].set_xlabel('X Axis', fontsize = 14)
        axes[i].set_xlim([95,130])
        axes[i].tick_params(labelsize=13)
        sns.kdeplot(dataset[f],dataset[f+1],cmap="Blues_d",shade_lowest=True,ax=axes[i])
        sns.kdeplot(dataset[f],dataset[f+1],cmap="Blues",shade_lowest=True,shade=True,ax=axes[i])
        f = f+2
    plt.show()

但是,一些轮廓在 x-y 平面中采样的空间有所不同,当我绘制它们时,一些图表的背景中有一个白色部分,我不知道如何填充。见下图:

两张图都应该看起来像位于右侧的图。我尝试使用

ax.set_facecolor
设置背景颜色,并借助以下线程获取 cmap 调色板的最后一种颜色:从 matplotlib 中的颜色图中获取单独的颜色。但是,我得到的背景颜色略有不同。我还尝试使用
sns.kdeplot
的剪辑参数,但没有成功。

我该如何解决这个问题?如果您需要更多信息,请告诉我。

python matplotlib seaborn
2个回答
0
投票

所以我做了一些修改,去掉了网格并使用 gimp 来获得最小的面部颜色。 (我使用截图工具获取图像的图片,在名为 gimp 的免费程序中打开它,并获取十六进制代码。)然后我更改了轴的面颜色。下面的代码会给你你想要的。

import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

def kdeplots_figure(dataset):
    cols = int((len(dataset)-2)/2)
    figure, axes = plt.subplots(1, cols, figsize=(15,6), sharey=True)

    f = 2
    for i in range (len(axes)):
        sns.set_style("white")
        axes[i].set_title(" Cluster {0}".format(i), fontsize = 16)
        axes[i].set_ylabel('Y Axis', fontsize = 14)
        axes[i].set_xlabel('X Axis', fontsize = 14)

        axes[i].tick_params(labelsize=13)
        axes[i].grid(False) 

        axes[i].set_facecolor('#eef5fc')
        sns.kdeplot(dataset[f],dataset[f+1],cmap="Blues_d",ax=axes[i])
        sns.kdeplot(dataset[f],dataset[f+1],cmap="Blues",  shade=True, 
        extend='both', ax=axes[i])

        f = f+2
    plt.show()

a = np.random.rand(6,6)
kdeplots_figure(a)


0
投票

一种更容易实现的 hack - 将“较大”KDE 图的极值点添加到“较小”图上。在尾部添加几个点不应改变 KDE 的整体形状。

最小示例:

foo = np.random.normal(0, 1, (1000, 2))
bar = np.random.normal(0, 0.5, (1000, 2))

fig, axs = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(10, 5))
sns.kdeplot(x=foo[:, 0], y=foo[:, 1], ax=axs[0], alpha=0.8, fill=True, thresh=0)
sns.kdeplot(x=bar[:, 0], y=bar[:, 1], ax=axs[1], alpha=0.8, fill=True, thresh=0)
for ax in axs:
    ax.set_xlim(-3, 3)
    ax.set_ylim(-3, 3)
plt.show()

给出以下内容:

# get max and min of each coordinate
max_x, min_x = np.max(foo[:, 0]), np.min(foo[:, 0])
max_y, min_y = np.max(foo[:, 1]), np.min(foo[:, 1])

# append the 4 possible corners
corners = 1.1 * np.array(
    [[max_x, max_y], [max_x, min_y], [min_x, max_y], [min_x, min_y]]
)
bar = np.concatenate([bar, corners], axis=0)

# plot new kde:
fig, axs = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(10, 5))
sns.kdeplot(x=foo[:, 0], y=foo[:, 1], ax=axs[0], alpha=0.8, fill=True, thresh=0)
sns.kdeplot(x=bar[:, 0], y=bar[:, 1], ax=axs[1], alpha=0.8, fill=True, thresh=0)
for ax in axs:
    ax.set_xlim(-3, 3)
    ax.set_ylim(-3, 3)
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.