在python中,如何将数字向量映射到颜色?
我很难找到一种方法将数字向量转换为“良好”色阶(ala matplotlib),这样我就可以只得到颜色值的向量。这对于在django中呈现一些静态html页面非常有用。有什么想法吗在python中,如何将数字向量映射到颜色?,python,django,matplotlib,Python,Django,Matplotlib,我很难找到一种方法将数字向量转换为“良好”色阶(ala matplotlib),这样我就可以只得到颜色值的向量。这对于在django中呈现一些静态html页面非常有用。有什么想法吗 谢谢各位的回答!真的很有帮助 您可以使用以下代码: from __future__ import division import numpy import matplotlib.cm as cmap r = numpy.random.rand(1000) #
谢谢各位的回答!真的很有帮助 您可以使用以下代码:
from __future__ import division
import numpy
import matplotlib.cm as cmap
r = numpy.random.rand(1000) #random numbers
c = cmap.jet(numpy.int_((r-r.min())/r.ptp()*255)) #colors
所以你试着:
>>> r
array([ 0.88741281, 0.61022571, 0.14819983, 0.3695846 , 0.73832029,
0.6266069 , 0.6917337 , 0.09162752, 0.83532511, 0.54001574, ...,])
>>> c
array([[ 1. , 0.08787219, 0. , 1. ],
[ 0.83175206, 1. , 0.13598988, 1. ],
[ 0. , 0.08039216, 1. , 1. ],
...,
[ 0. , 0.72352941, 1. , 1. ],
[ 0.24984187, 1. , 0.71790006, 1. ],
[ 1. , 0.45098039, 0. , 1. ]])
这是RGBA格式的每一行的颜色。您也可以查看。它提供了一些将有效matplotlib颜色转换为RGB或RGBA的方法 有效的matplotlib颜色为
映射不一定是唯一的。您可以创建任何您喜欢的映射。