python - 将离散值映射到颜色

标签 python matplotlib

我正在尝试基于 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')

enter image description here

关于python - 将离散值映射到颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30585096/

相关文章:

python - 使用 Matplotlib imshow() 显示缩放 = 1 的图像(如何?)

python - 如何在我自己的模板中使用内置的密码重置/更改 View

python - 如何限制在亚马逊博托中获取所有图像的输出?

python - 找不到包 "libapache2-mod-proxy-html"Ubuntu 16.04

python - DCGAN 中矩阵大小不兼容 : In[0]: [16, 1024]、In[1] : [16384, 1]

python - apache 不提供除 '/' 之外的 Flask 路由

python - 如何使用matplotlib绘制pyspark sql结果

python - 请详细解释Python中的二维直方图

python-3.x - PyCharm 导入错误 : Claims 'matplotlib' is not a package, 但在 IDLE 中成功运行

python - 该图不是在 matplotlib 中绘制的