在python中如何将数字向量映射到颜色?

2024-10-01 19:32:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难找到一种方法来将一个数字向量转换成一个“好”的色阶(alamatplotlib),这样我就可以只得到颜色值的向量。这对于在django中呈现一些静态html页面非常有用。有什么想法吗?你知道吗

谢谢各位的回答!真的很有帮助!你知道吗


Tags: django方法颜色html静态数字页面向量
2条回答

您还可以查看ColorConverter。它提供了一些将有效的matplotlib颜色转换为RGB或RGBA的方法。你知道吗

有效的matplotlib颜色为

  1. a letter from the set ‘rgbcmykw’
  2. a hex color string, like ‘#00FFFF’
  3. a standard name, like ‘aqua’ a float, like ‘0.4’, indicating gray on
  4. a 0-1 scale

您可以使用以下代码:

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格式显示每行的颜色。你知道吗

相关问题 更多 >

    热门问题