加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 将离散值映射到颜色

发布时间:2020-12-20 11:43:25 所属栏目:Python 来源:网络整理
导读:我试图基于4个离散值1,2,3,4创建pcolor.我想将1定义为黑色,2定义为红色,3定义为黄色,4定义为绿色.有谁知道怎么做? test = ([1,1,3],[1,4],[2,1])import numpy as npdataset = np.array(test)plt.pcolor(dataset) 谢谢, 解决方法 我认为你不想使用pcolor. Fr
我试图基于4个离散值1,2,3,4创建pcolor.我想将1定义为黑色,2定义为红色,3定义为黄色,4定义为绿色.有谁知道怎么做?

test = ([1,1,3],[1,4],[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),2:(1,3:(1,4:(0,0)}
test = ([1,1])
test_rgb = [[colordict[i] for i in row] for row in test]
plt.imshow(test_rgb,interpolation = 'none')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读