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

标签 python matplotlib seaborn subplot color-palette

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

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

但是,此方法不适用于 seaborn.palplot ,可视化 seaborn 调色板。我的目标是创建一个不同调色板的图形,用于可扩展的颜色比较和演示。此 image大致显示了我正在尝试创建的图 [ 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 值,这些值均匀分布在 palplots 上,但由于不寻常的绘图方式,我不知道这是否容易实现在 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())
因此,它不会返回任何轴或图形对象,也不会允许您指定要写入的轴对象。您可以通过添加 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)
example results

关于python - 将 seaborn.palplot 轴添加到现有图形中以可视化不同的调色板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50516094/

相关文章:

python - 将库尔德文本(从右到左)添加到图像会返回分隔的字符串

python - 如何将两个程序合并为一个程序?

python - Matplotlib 中的 Unicode 字符串注释

python - 如何理解Seaborn的热图注释格式?

matplotlib - seaborn "kde jointplot"在最新版本 (0.11.0) 中没有颜色映射

python - session 未创建异常 : Message: Unable to create new service: ChromeDriverService with ChromeDriver and SeleniumGrid through Python

python-pptx:将图片插入占位符但出现错误

python - 删除 matplotlib 图中的空子图

python - 使用 numpy/scipy 创建一个分配给特定整数值的颜色图

python - 在 Seaborn boxplot 中设置传单(异常值)样式被忽略