将整数值的范围映射到python中的颜色 [英] Mapping range of integer values to colors in python

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

问题描述

我有从 0 到 10000 的整数.我想将颜色映射到其中的每一个.然后基于整数值,我想检索与整数值相对应的颜色的RGB等效项.基本上我想在两种或多种颜色之间产生插值效果,例如如果颜色是绿色和红色,则绿色权重最小(0),红色权重最高(10000).我如何使用 matplotlib 来实现这个映射,或者是否有其他相同的库.

I have integers ranging from 0 to 10000. I want to map a color to each of these. Then based on the integer value i want to retrieve RGB equivalent of the color corresponding to integer value. Basically i want to have an interpolation effect between two or more colors e.g. if the colors are green and red,then green having least weight(0) and red having having highest weight (10000). how can i implement this mapping using matplotlib or is there any other library for the same.

推荐答案

确实可以从给定的颜色图中采样 10000 种颜色:

It is indeed possible to sample 10000 colors from a given colormap:

#!/usr/bin/python3

from numpy import arange

from matplotlib import pyplot as plt
from matplotlib import cm
from matplotlib.colors import LinearSegmentedColormap


# ======
## data:

N = 10000
data = arange(N +1)


# =================
## custom colormap:

# red-green colormap:
cdict = {'red':   [(0.0, 1.0, 1.0),  # red decreases
                   (1.0, 0.0, 0.0)],

         'green': [(0.0, 0.0, 0.0),  # green increases
                   (1.0, 1.0, 1.0)],

         'blue':  [(0.0, 0.0, 0.0),  # no blue at all
                   (1.0, 0.0, 0.0)]}

red_green_cm = LinearSegmentedColormap('RedGreen', cdict, N)


# ======
## plot:

colors = cm.get_cmap(red_green_cm, N)

fig = plt.figure()
ax = fig.add_subplot(111)

# each line is of its own color:
for i, x in enumerate(data):
    ax.plot(data, data*x, color=colors(i))

fig.savefig("red-green-cm.png")

结果:

编辑

还可以添加一个颜色栏:

One can also add a colorbar:

#!/usr/bin/python3

from numpy import arange

import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import cm
from matplotlib.colors import LinearSegmentedColormap


# ======
## data:

N = 10000
data = arange(N +1)


# =================
## custom colormap:

# red-green colormap:
cdict = {'red':   [(0.0, 1.0, 1.0),  # red decreases
                   (1.0, 0.0, 0.0)],

         'green': [(0.0, 0.0, 0.0),  # green increases
                   (1.0, 1.0, 1.0)],

         'blue':  [(0.0, 0.0, 0.0),  # no blue at all
                   (1.0, 0.0, 0.0)] }

red_green_cm = LinearSegmentedColormap('RedGreen', cdict, N)


# ======
## plot:

colors = cm.get_cmap(red_green_cm, N)

fig = plt.figure()
ax = fig.add_subplot(111)

# each line is of its own color:
for i, x in enumerate(data):
    ax.plot(data, data*x, color=colors(i))

# make space for colorbar:
fig.tight_layout(rect=[0, 0, 0.85, 1])

# adding colorbar:
ax_cb = fig.add_axes([0.85, 0.10, 0.05, 0.8])
norm = mpl.colors.Normalize(vmin=data[0], vmax=data[-1])
cb = mpl.colorbar.ColorbarBase(ax_cb, cmap=red_green_cm, norm=norm, orientation='vertical')

fig.savefig("red-green-cm.png")

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

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