【问题标题】:Custom coloration for matrix in matplotlibmatplotlib 中矩阵的自定义着色
【发布时间】:2015-07-04 08:29:27
【问题描述】:

我想绘制三个矩阵,但我想出的唯一解决方案是一个接一个地绘制,这让我只绘制了最后一个矩阵。

ax.imshow(mat1, cmap='Blues', interpolation='nearest')
ax.imshow(mat2, cmap='binary', interpolation='nearest')
ax.imshow(mat3, cmap='autumn', interpolation='nearest')  # actual plot

我想要的是在三个矩阵中以白色显示所有 0,并根据矩阵以不同的色调显示更高的值,例如:蓝色、黑色和红色。此外,在该示例中,红色单元格将优先于黑色,黑色优先于蓝色。我想象的解决方案是一个函数,给定一个三元组(蓝色,黑色,红色),每个组件具有不同的值,返回单元格应该着色的颜色,并将其提供给 ColorMap,但我真的不知道该怎么做,也不知道有没有可能。

欢迎和赞赏各种帮助甚至不同的解决方案(这是最有可能发生的)。提前致谢。

【问题讨论】:

    标签: python matrix matplotlib plot colormap


    【解决方案1】:

    您想要一个 第四 图像,每个点的 RGB 值是对应点的前三个矩阵的单个值的函数?如果是这样,您能否生成从三个值到 RGB 第四个值的代数?

    您的问题表明对绘图如何将数据转换为颜色的困惑。颜色图采用单值数据,对其进行规范化,并将其映射到一些命名的颜色数组中。 0 值可能会映射到任何颜色,具体取决于颜色图和其余数据。

    位图定义每个像素的(红、绿、蓝)值。正确的位图有标题部分,但数据是 (m,n,3) 数组。 imshow 仅绘制该数组;它期望 RGB 值在 [0,1] 范围内。

    如果您有三个数据矩阵,则必须选择如何将值映射到 RGB 值。这是一个将三种映射到 RGB 的示例。前两行是具有一系列值的虚拟数据,以颜色图或最简单的 RGB 表示形式显示。最后一行显示了使用整个色彩空间将所有三个虚拟矩阵组合成一个图像的方法。

    # -*- coding: utf-8 -*-
    import matplotlib.pyplot as plt
    import numpy as np
    
    #dummy data
    x = 8
    y = 15
    mat = []
    mat.append(np.arange(x * y).reshape((x, y)) / float(x * y) )
    mat.append(np.arange(x * y).reshape((y, x)).T / float(x* y))
    mat.append(np.arange(y) * np.arange(x)[:,np.newaxis] / float(99))
    
    # NOTE: this data is approximately in the RGB range. If yours isn't, normalize,
    # here or in your makeRGB function.
    # (The colormap normalizes single-valued data).
    
    fig, axs = plt.subplots(figsize=(7,4), nrows=3, ncols=3,
                            gridspec_kw={'hspace':0.6})
    
    axs[0,0].imshow(mat[0], cmap='Reds', interpolation='nearest')
    axs[0,1].imshow(mat[1], cmap='Greens', interpolation='nearest')
    axs[0,2].imshow(mat[2], cmap='Blues', interpolation='nearest')
    axs[0,0].set_xlabel('Reds Colormap')
    axs[0,1].set_xlabel('Greens Colormap')
    axs[0,2].set_xlabel('Blues Colormap')
    
    def asOneHue(mat, hue):
        """
        Use a single-valued matrix to represent one hue in a RGB file.'
        """
        RGBout = np.zeros((len(mat),len(mat[0]),3))
        RGBout[:,:,i] = mat
        return RGBout
    
    for i in (0,1,2):
        axs[1,i].imshow(asOneHue(mat[i],i))
    axs[1,0].set_xlabel('Reds bitmap')
    axs[1,1].set_xlabel('Greens bitmap')
    axs[1,2].set_xlabel('Blues bitmap')
    
    # different ways to combine 3 values
    def makeRGB0(mats):
        RGBout = np.zeros((len(mats[0]),len(mats[0][0]),3))
        #RGBout = np.ones((len(mats[0]),len(mats[0][0]),3))
        for i in (0,1,2):
            RGBout[:,:,i] = mats[i]
        return RGBout
    
    axs[2,0].imshow(makeRGB0(mat))
    axs[2,0].set_xlabel('Color layers')
    
    def makeRGB1(mats):
        RGBout = np.zeros((len(mats[0]),len(mats[0][0]),3))
        i,j,k = RGBout.shape
    
        for x in range(i):
            for y in range(j):
                RGBout[x,y] = (mats[0][x][y] / 2,
                               mats[1][x][y],
                               1 - mats[2][x][y])
        return RGBout
    
    
    axs[2,1].imshow(makeRGB1(mat))
    axs[2,1].set_xlabel('Algebraic')
    
    def makeRGB2(mats):
        RGBout = np.zeros((len(mats[0]),len(mats[0][0]),3))
        i,j,k = RGBout.shape
        for x in range(i):
            for y in range(j):
                if mats[0][x][y] > .8:
                    RGBout[x,y] = (mats[0][x][y],
                                   0,
                                   0)
                elif mats[1][x][y] > .8:
                        RGBout[x,y] = (0,
                                       mats[1][x][y],
                                       0)
                else:
                        RGBout[x,y] = (mats[0][x][y],
                                       mats[1][x][y],
                                       mats[2][x][y])
        return RGBout
    
    axs[2,2].imshow(makeRGB2(mat))
    axs[2,2].set_xlabel('If-else')
    
    plt.show()
    

    【讨论】:

    • 非常感谢您的回答,我在文档中没有发现我可以绘制一个普通的 RGB 矩阵——这真的让我很开心!
    • 酷。我自己也遇到过这个问题的一部分;我希望这些示例中隐含了主要缺陷。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 2015-03-28
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多