代码之家  ›  专栏  ›  技术社区  ›  saran3h

python matplotlib-颜色代码+ve和-ve值在绘图中

  •  -1
  • saran3h  · 技术社区  · 7 年前

    我有一堆形状(1104)的样品。所有样本都是整数(+ve、-ve和0),在 imshow 功能 matplotlib . 下面是我创建的将它们显示为图像的函数。

    def show_as_image(sample):
        bitmap = sample.reshape((13, 8))
        plt.figure()
        # this line needs changes.
        plt.imshow(bitmap, cmap='gray', interpolation='nearest')
        plt.colorbar()
        plt.show()
    

    我需要 色码 sample . PS:取0为正。 如何更改代码?

    2 回复  |  直到 7 年前
        1
  •  1
  •   ImportanceOfBeingErnest    7 年前

    您可以设置颜色编码的标准化,使其在数据的负绝对值和正绝对值之间平均分布。使用中间带有浅色的颜色映射有助于可视化值离零的距离。

    导入numpy as np 将matplotlib.pyplot导入为plt def显示为图像(示例): 位图=sample.remaze((13,8)) maxval=np.max(np.abs([bitmap.min(),bitmap.max()])) 图() plt.imshow(位图,cmap='rdylgn',interpolation='nearest', vmin=-maxval,vmax=maxval) 色标() 显示() 样本=np.随机.randn(1104) 显示为图像(示例) < /代码>

    如果需要二进制映射,则可以将正值映射为1,将负值映射为0。

    导入numpy as np 将matplotlib.pyplot导入为plt def显示为图像(示例): 位图=sample.remaze((13,8)) 位图[Bitmap>=0]=1 位图[位图<0]=0 图() plt.imshow(位图,cmap='rdylgn',interpolation='nearest', vmin=-.1,vmax=1.1) 显示() 样本=np.随机.randn(1104) 显示为图像(示例) < /代码>

    在这种情况下,使用颜色条可能是无用的。

    数据。使用中间带有浅色的颜色映射可以帮助可视化值离零的距离。

    import numpy as np
    import matplotlib.pyplot as plt
    
    def show_as_image(sample):
        bitmap = sample.reshape((13, 8))
        maxval = np.max(np.abs([bitmap.min(),bitmap.max()]))
        plt.figure()
        plt.imshow(bitmap, cmap='RdYlGn', interpolation='nearest',
                   vmin=-maxval, vmax=maxval)
        plt.colorbar()
        plt.show()
    
    sample=np.random.randn(1,104)
    show_as_image(sample)
    

    enter image description here

    如果需要二进制映射,则可以将正值映射为1,将负值映射为0。

    import numpy as np
    import matplotlib.pyplot as plt
    
    def show_as_image(sample):
        bitmap = sample.reshape((13, 8))
        bitmap[bitmap >= 0] = 1
        bitmap[bitmap < 0] = 0
        plt.figure()
        plt.imshow(bitmap, cmap='RdYlGn', interpolation='nearest',
                   vmin=-.1, vmax=1.1)
        plt.show()
    
    sample=np.random.randn(1,104)
    show_as_image(sample)
    

    enter image description here

    在这种情况下,使用颜色条可能是无用的。

        2
  •  1
  •   Mr. T Andres Pinzon    7 年前

    您可以创建一个三维数组,为每个像素指定颜色代码。因此,如果您想要黑白,您将分别通过 (0,0,0). (1,1,1). 。这样的方法应该有效:

    def show_as_image(sample):
    位图=sample.remaze((13,8))
    位图颜色=np.零((13,8,3))
    Bitmap_colored[Bitmap>=0]=[1,1,1]黑色,用于大于或等于0的值
    位图颜色[位图<0]=[0,0,0]白色,适用于小于0的值
    图()
    plt.imshow(位图着色,插值为“最近的”)。
    显示()
    < /代码> 
    
    

    例如:

    >>>sample=np.random.randint(low=-10,high=10,size=(1104))。
    >>>显示为图像(示例)
    < /代码> 
    
    

    将输出如下内容:

    (1,1,1),分别。这样的方法应该有效:

    def show_as_image(sample):
        bitmap = sample.reshape((13, 8))
        bitmap_colored = np.zeros((13,8,3))
        bitmap_colored[bitmap>=0] = [1,1,1] # black for values greater or equal to 0
        bitmap_colored[bitmap<0] = [0,0,0] # white for values less than 0
        plt.figure()
        plt.imshow(bitmap_colored, interpolation='nearest')
        plt.show()
    

    例如:

    >>> sample = np.random.randint(low=-10,high=10,size=(1,104))
    >>> show_as_image(sample)
    

    将输出如下内容: enter image description here