Matplotlib颜色图显示不正确的颜色

时间:2020-10-23 13:39:15

标签: python numpy matplotlib

我需要制作一个从红色到白色具有256种颜色的色图,并在Python中显示红色通道,但是看起来这件事做错了,我不明白为什么。这是我的代码:

for

1 个答案:

答案 0 :(得分:2)

您可以这样回答:

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(256):
    colormap[i,0] = 1
    colormap[i,1] = (i+1)/256
    colormap[i,2] = (i+1)/256
    
#display the thing:
colormap = mpl.colors.ListedColormap(colormap)
plt.figure(), plt.imshow(img, cmap = colormap)

几乎就像您在Colormap it's not composed of correct color中所做的那样。 您只需要编写代码的第二部分(从红色到白色),并以256步而不是128步来完成。