简体   繁体   English

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

[英]Matplotlib Colormap showing Incorrect Color

I need to make a colormap with 256 colors from red to white and display the red channel in Python but it looks like this thing it's done wrong and I don't understand why.我需要制作一个从红色到白色的 256 种颜色的颜色图,并在 Python 中显示红色通道,但看起来这件事做错了,我不明白为什么。 This is my code:这是我的代码:

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])
colormap1 = np.zeros([256,1])
#image:
for i in range(256):
    img[:,i] = i #on all columns I have the same value
    #to go from red to white we'll have: [1,0,0]...,[1,0.5,0.5],..[1,1,1]
for i in range(128):
    colormap1[i,1] = i/127     

#display the thing:  
colormap1 = mpl.colors.ListedColormap(colormap1)
plt.figure(), plt.imshow(img, cmap = colormap1)

You can answer it like that :你可以这样回答:

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)

almost like you did in here Colormap it's not composed of correct color .几乎就像你在这里做的Colormap 它不是由正确的 color 组成的 You just need to write the second part of your code (from red to white) and do it in 256 moves instead of 128.您只需要编写代码的第二部分(从红色到白色),并在 256 次而不是 128 次移动中完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM