将 seaborn.palplot 轴添加到现有图形以可视化不同的调色板

Add seaborn.palplot axes to existing figure for visualisation of different color palettes

将 seaborn 图形添加到子图是 usually 通过在创建图形时传递 'ax' 来完成的。例如:

sns.kdeplot(x, y, cmap=cmap, shade=True, cut=5, ax=ax)

但是,此方法不适用于 seaborn.palplot, which visualizes seaborn color palettes. My goal is to create a figure of different color palettes for scalable color comparison and presentation. This image roughly shows the figure I'm trying to create [source]。

一个可能相关的 answer 描述了一种创建 seaborn 图形并将轴复制到另一个图形的方法。我一直没能把这个方法应用到palplot图形上,想知道是否有快速的方法可以将它们强制到现有图形中。

这是我的最小工作示例,现在仍在生成单独的数字。

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

fig1 = plt.figure()
length, n_colors = 12, 50  # amount of subplots and colors per subplot
start_colors = np.linspace(0, 3, length)
for i, start_color in enumerate(start_colors):
    ax = fig1.add_subplot(length, 1, i + 1)
    colors = sns.cubehelix_palette(n_colors=n_colors, start=start_color,
                                   rot=0, light=0.4, dark=0.8)
    sns.palplot(colors)
plt.show(fig1)

最终,为了让情节更丰富,打印存储在颜色中的 RGB 值(类似列表)在触感图上均匀分布会很好,但我不知道这是否容易实现,因为palplot 中不寻常的绘图方式。

如有任何帮助,我们将不胜感激!

您可能已经发现,palplot 函数的文档很少,但我直接从 seaborn github repo 此处提取:

def palplot(pal, size=1):
    """Plot the values in a color palette as a horizontal array.
    Parameters
    ----------
    pal : sequence of matplotlib colors
        colors, i.e. as returned by seaborn.color_palette()
    size :
        scaling factor for size of plot
    """
    n = len(pal)
    f, ax = plt.subplots(1, 1, figsize=(n * size, size))
    ax.imshow(np.arange(n).reshape(1, n),
              cmap=mpl.colors.ListedColormap(list(pal)),
              interpolation="nearest", aspect="auto")
    ax.set_xticks(np.arange(n) - .5)
    ax.set_yticks([-.5, .5])
    # Ensure nice border between colors
    ax.set_xticklabels(["" for _ in range(n)])
    # The proper way to set no ticks
    ax.yaxis.set_major_locator(ticker.NullLocator())

因此,它不会 return 任何轴或图形对象,也不允许您指定要写入的轴对象。您可以创建自己的,如下所示,通过添加 ax 参数和条件以防未提供。根据上下文,您可能还需要包含的导入。

def my_palplot(pal, size=1, ax=None):
    """Plot the values in a color palette as a horizontal array.
    Parameters
    ----------
    pal : sequence of matplotlib colors
        colors, i.e. as returned by seaborn.color_palette()
    size :
        scaling factor for size of plot
    ax :
        an existing axes to use
    """

    import numpy as np
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import matplotlib.ticker as ticker

    n = len(pal)
    if ax is None:
        f, ax = plt.subplots(1, 1, figsize=(n * size, size))
    ax.imshow(np.arange(n).reshape(1, n),
              cmap=mpl.colors.ListedColormap(list(pal)),
              interpolation="nearest", aspect="auto")
    ax.set_xticks(np.arange(n) - .5)
    ax.set_yticks([-.5, .5])
    # Ensure nice border between colors
    ax.set_xticklabels(["" for _ in range(n)])
    # The proper way to set no ticks
    ax.yaxis.set_major_locator(ticker.NullLocator())

当您包含 'ax' 参数时,该函数应该像您预期的那样工作。要在您的示例中实现这一点:

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

fig1 = plt.figure()
length, n_colors = 12, 50  # amount of subplots and colors per subplot
start_colors = np.linspace(0, 3, length)
for i, start_color in enumerate(start_colors):
    ax = fig1.add_subplot(length, 1, i + 1)
    colors = sns.cubehelix_palette(
        n_colors=n_colors, start=start_color, rot=0, light=0.4, dark=0.8
    )
    my_palplot(colors, ax=ax)
plt.show(fig1)