将离散值映射到颜色 [英] Map discrete value to color

查看:44
本文介绍了将离散值映射到颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于 4 个离散值 1、2、3、4 创建 pcolor.我想将 1 定义为黑色,2 为红色,3 为黄色,4 为绿色.有人知道怎么做吗?

I am trying to create pcolor based on 4 discrete value 1, 2, 3, 4. I want to define 1 as black, 2 as red, 3 as yellow and 4 as green. does anyone know how to do it?

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.从Matlab文档中(大概是matplotlib pcolor做同样的事情):

I don't think you want to use pcolor. From the Matlab docs (presumably the matplotlib pcolor does the same thing):

C的最小和最大元素在颜色图中被指定为第一和最后一个颜色.C中其余元素的颜色是通过从值到颜色图元素的线性映射来确定的.

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 来映射您想要的颜色:

You could try imshow instead, and use a dict to map the colors you want:

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')

这篇关于将离散值映射到颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆