我正在尝试基于 4 个离散值 1、2、3、4 创建 pcolor。我想将 1 定义为黑色,2 为红色,3 为黄色,4 为绿色。有人知道怎么做吗?
test = ([1,2,2,1,3],[1,1,1,1,4],[2,1,1,2,1])
import numpy as np
dataset = np.array(test)
plt.pcolor(dataset)
谢谢,
最佳答案
我认为您不想使用 pcolor。 From the Matlab docs (大概 matplotlib pcolor 做了同样的事情):
The minimum and maximum elements of C are assigned the first and last colors in the colormap. Colors for the remaining elements in C are determined by a linear mapping from value to colormap element.
您可以尝试使用 imshow
,并使用 dict
来映射您想要的颜色:
colordict = {1:(0,0,0),2:(1,0,0),3:(1,1,0),4:(0,1,0)}
test = ([1,2,2,1,3],[1,1,1,1,4],[2,1,1,2,1])
test_rgb = [[colordict[i] for i in row] for row in test]
plt.imshow(test_rgb, interpolation = 'none')
关于python - 将离散值映射到颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30585096/