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

2024-05-21

将seaborn人物添加到子图中是usually https://seaborn.pydata.org/examples/cubehelix_palette.html创建图形时通过传递“ax”来完成。例如:

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

但该方法不适用于seaborn.palplot https://seaborn.pydata.org/generated/seaborn.palplot.html,它可视化seaborn调色板。我的目标是创建一个不同调色板的图形,以进行可扩展的颜色比较和演示。这image https://i0.wp.com/edwards.sdsu.edu/research/wp-content/uploads/2017/03/seaborn_palettes.png?ssl=1粗略地显示了我正在尝试创建的图形[source https://edwards.sdsu.edu/research/python-dataviz-seaborn-heatmap-palettes/].

一个可能相关的answer https://stackoverflow.com/a/47664533/5511061描述了创建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 值均匀地分布在 palplots 上,但我不知道这是否容易实现,因为绘图的方式不寻常在 palplot 中。

任何帮助将不胜感激!


正如您可能已经发现的那样,关于 palplot 函数的文档很少,但我直接从Seaborn github 仓库 https://github.com/mwaskom/seaborn/blob/ceee0324f9b352c6d4aa9eb145d6ad3f5bcaad41/seaborn/miscplot.py here:

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

因此,它不返回任何轴或图形对象,也不允许您指定要写入的轴对象。您可以通过添加 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐