【问题标题】:White space while plotting multiple filled contours subplots using kdeplots from seaborn使用 seaborn 的 kdeplots 绘制多个填充轮廓子图时的空白
【发布时间】:2018-05-02 16:51:07
【问题描述】:

我编写了一个 python 函数,用于绘制一系列填充轮廓,使用 seabornkdeplot 函数从由 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 调色板的最后一种颜色:Getting individual colors from a color map in matplotlib。但是,我得到的背景颜色略有不同。我还尝试使用sns.kdeplot 的剪辑参数,但没有成功。

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

【问题讨论】:

  • 像往常一样,minimal reproducible example 会很有帮助,这样人们就可以复制、粘贴和运行代码。
  • 确实,dataset 及其内容是什么?熊猫系列还是数据框? numpy 数组?
  • 是一个numpy数组列表
  • 你找到解决办法了吗?

标签: python matplotlib seaborn


【解决方案1】:

所以我做了一些修改,摆脱了网格并使用 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    相关资源
    最近更新 更多