以十六进制格式提取matplotlib颜色表 [英] Extract matplotlib colormap in hex-format

查看:988
本文介绍了以十六进制格式提取matplotlib颜色表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过处理这个例子来从matplotlib颜色表中提取离散的颜色。但是,我无法找到从颜色映射中提取的 N 离散颜色。

在下面的代码中,'I $'我使用了 cmap._segmentdata ,但我发现它是整个色彩映射的定义。给定一个颜色表和一个整数 N ,如何从颜色表中提取 N 离散颜色并以十六进制格式导出它们格式?

  from pylab import * 

delta = 0.01
x = arange(-3.0 ,3.0,delta)
y = arange(-3.0,3.0,delta)
X,Y = meshgrid(x,y)
Z1 = bivariate_normal(X,Y,1.0,1.0,0.0 ,0.0)
Z2 = bivariate_normal(X,Y,1.5,0.5,1,1)
Z = Z2 - Z1#高斯差分

cmap = cm.get_cmap( ''seismic',5)#PiYG
cmap_colors = cmap._segmentdata

def print_hex(r,b,g):
如果不是(0 <= r < 255或0 <= b <= 255或0 <= g <= 255):
raise ValueError('rgb not in range(256)')
print'#%02x% (len)(cmap_colors ['blue'])):
r = int(cmap_colors [',b,g)


红'] [i] [2] * 255)
b = int(cmap_colors ['blue'] [i] [2] * 255)
g = int(cmap_colors ['green'] [i] [2] * 255)
print_hex(r,g,b)



im = imshow(Z,cmap = cmap,interpolation ='bilinear',
vmax = abs(Z).max(),vmin = -abs(Z).max())
axis ('off')
colorbar()

show()


cmap(i)获得索引为 i 的段的rgba值的元组。 )。还有一个将rgb值转换为十六进制的函数。正如Joe Kington在评论中所写,您可以使用 matplotlib.colors.rgb2hex 。因此,一个可能的解决方案是:

  from pylab import * 

cmap = cm.get_cmap( '地震',5)#PiYG

我在范围内(cmap.N):
rgb = cmap(i)[:3]#将返回rgba,我们只取前3所以我们得到rgb
print(matplotlib.colors.rgb2hex(rgb))

输出是:

 #00004c 
#0000ff
#ffffff
#ff0000
#7f0000


I am trying to extract discrete colors from a matplotlib colormap by manipulating this example. However, I cannot find the N discrete colors that are extracted from the colormap.

In the code below I've used cmap._segmentdata, but I've found that it is the definition of the entire colormap. Given a colormap and an integer N, how do I extract N discrete colors from the colormap and export them in hex-format?

from pylab import *

delta = 0.01
x = arange(-3.0, 3.0, delta)
y = arange(-3.0, 3.0, delta)
X,Y = meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = Z2 - Z1 # difference of Gaussians

cmap = cm.get_cmap('seismic', 5)    # PiYG
cmap_colors = cmap._segmentdata

def print_hex(r,b,g):
               if not(0 <= r <= 255 or 0 <= b <= 255 or 0 <= g <= 255):
                              raise ValueError('rgb not in range(256)')
               print '#%02x%02x%02x' % (r, b, g)


for i in range(len(cmap_colors['blue'])):
               r = int(cmap_colors['red'][i][2]*255)
               b = int(cmap_colors['blue'][i][2]*255)
               g = int(cmap_colors['green'][i][2]*255)
               print_hex(r, g, b)



im = imshow(Z, cmap=cmap, interpolation='bilinear',
            vmax=abs(Z).max(), vmin=-abs(Z).max())
axis('off')
colorbar()

show()

解决方案

You can get a tuple of rgba values for the segment with index i by calling cmap(i). There is also already a function that turns rgb values into hex. As Joe Kington wrote in the comments, you can use matplotlib.colors.rgb2hex. Therefore, a possible solution would be:

from pylab import *

cmap = cm.get_cmap('seismic', 5)    # PiYG

for i in range(cmap.N):
    rgb = cmap(i)[:3] # will return rgba, we take only first 3 so we get rgb
    print(matplotlib.colors.rgb2hex(rgb))

The output is:

#00004c
#0000ff
#ffffff
#ff0000
#7f0000

这篇关于以十六进制格式提取matplotlib颜色表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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