颜色图不是由正确的颜色组成

时间:2020-10-23 12:59:15

标签: python numpy matplotlib

我需要制作一个从黑色到红色到白色具有256种颜色的色图,并在Python中显示红色通道,但它看起来像是唯一一个从黑色到红色显示的通道,我不明白为什么...还有红色通道只有它不显示。这是我的代码:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

# How to create an array filled with zeros
img = np.zeros([256,256])
colormap = np.zeros([256,3])

#image:
for i in range(256):
    img[:,i] = i #on all columns I have the same value
    
#color map:
for i in range(128):
    colormap[i,0] = i/127 
    colormap[i+128,0] = 1 #from line 128 incolo I have 1 on first position because I've already reached red
    colormap[i+128,2] = (i+1)/128
    
#display the thing:
colormap = mpl.colors.ListedColormap(colormap)
plt.figure(), plt.imshow(img, cmap = colormap)

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该在代码中添加以下行:

colormap[i+128,1] = (i+1)/128

您不能拥有白色,因为绿色值始终等于0。

要从红色变为白色,您应该具有以下RGB值:

[1,0,0]
...
[1,0.5,0.5]
...
[1,1,1]