Python 在matplotlib中显示带有RGB彩色单元格的矩阵状图像
我有一个3种(或更多)颜色的序列存储为RGB值(或相应的十六进制),我想将它们显示如下: 根据这些建议并进行修改,我能够稍微接近这些建议,尽管我不太确定我是否理解颜色是如何以单个浮点数表示的。我是否可以将RGB/hex表示转换为matshow()使用的任何形式?或者,是否有更优雅的方法来产生上述输出? 谢谢 有一个名为的包装器,它位于matplotlib的顶部,可以很好地显示颜色映射或选定的颜色: 我建议使用标准matplotlib,因为它提供了对颜色方案和转换的更多支持,如问题的另一部分所述。如果不想使用外部库,只需打印以下内容:Python 在matplotlib中显示带有RGB彩色单元格的矩阵状图像,python,matplotlib,Python,Matplotlib,我有一个3种(或更多)颜色的序列存储为RGB值(或相应的十六进制),我想将它们显示如下: 根据这些建议并进行修改,我能够稍微接近这些建议,尽管我不太确定我是否理解颜色是如何以单个浮点数表示的。我是否可以将RGB/hex表示转换为matshow()使用的任何形式?或者,是否有更优雅的方法来产生上述输出? 谢谢 有一个名为的包装器,它位于matplotlib的顶部,可以很好地显示颜色映射或选定的颜色: 我建议使用标准matplotlib,因为它提供了对颜色方案和转换的更多支持,如问题的另一部分所
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])
ax.set_xticklabels([])
ax.set_yticklabels([])
这个输出看起来很棒,感谢您对这个模块的引用(看起来非常有用!)。有没有一种方法可以显式地将一系列颜色传递给此函数,并让它打印它们,而不是选择调色板?没有关系!我想我明白了。很好的建议。谢谢大家!@Everaldaguiar我不太清楚,但是你可以使用
palplot
发布的代码,并输入ListedColormap
和离散的rgb颜色序列。如果这样做,请确保将“框”的数量设置为输入的大小,此函数将插值。如果您依赖于某种自动缩放魔术,最好直接使用set_xlim
和set_ylim
。@tcaswell“您正在依赖”=>“seaborn authors正在依赖”。这是直接从源代码中复制的(参见答案中的链接)。我把它贴在这里是为了给OP一个起点,尽管你的建议很好——也许吧?啊,没有完全理解代码证明。你应该调整你的答案;)
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])
ax.set_xticklabels([])
ax.set_yticklabels([])