简体   繁体   English

离散色彩图无法正常工作?

[英]Discrete colormap not working properly?

I have a range of values from 0 to ~450 and I want to make a discrete colormap where a range of values will always be represented by a particular colour. 我的取值范围是0到〜450,我想制作一个离散的色图,其中的取值范围将始终由一种特定的颜色表示。

I'm defining my colours like this: 我正在定义我的颜色,如下所示:

red = np.array([0, 0, 221, 239, 235, 248, 239, 234, 228, 222, 205, 196, 161, 147, 126, 99, 87, 70, 61]) / 256.
green = np.array([16, 217, 242, 240, 255, 225, 190, 160, 128, 87, 72, 59, 33, 21, 29, 30, 30, 29, 26]) / 256.
blue = np.array([255, 255, 243, 82, 11, 1, 63, 37, 39, 21, 27, 23, 22, 26, 29, 28, 27, 25, 22]) / 256.
colors = np.array([red, green, blue]).T

cmap = mpl.colors.ListedColormap(colors)
bounds = np.arange(0,450,23) # as I understand need to be num colors + 1
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

plot = iplt.contourf(to_plot, red.size, cmap=cmap)\
# as many contours as there are colours

From the above if my understanding is correct the 1st color (dark blue) needs to be mapped for the values 0 to 23. However what I see is: 从上面的内容,如果我的理解正确,则需要将第一种颜色(深蓝色)映射为0到23的值。但是,我看到的是:

在此处输入图片说明

The range 0-23 doesn't appear on this plot so the colour dark blue shouldn't either yet it does? 范围0-23不在此图上显示,因此深蓝色也不应该吗? What am I doing wrong? 我究竟做错了什么?

EDIT: Here's what happens when i add the norm kw: 编辑:这是当我添加标准 kw时发生的事情:

在此处输入图片说明 The bins get messed up? 垃圾箱弄乱了吗?

FINAL EDIT: It works now, here's what I've done: 最后编辑:现在可以使用,这是我所做的:

#No changes here:
new_cols = np.load(os.path.expanduser('~/Desktop/dl/colormap.npy'))
new_cmap = mpl.colors.ListedColormap(new_cols)
new_bounds = np.linspace(0,420,21)
new_norm = mpl.colors.BoundaryNorm(new_bounds,new_cmap.N)
plot = iplt.contourf(to_plot, 20, cmap=new_cmap, norm=new_norm)

#This is different:
cax, kw = mpl.colorbar.make_axes(ax, orientation='horizontal')
cbar = mpl.colorbar.ColorbarBase(cax, cmap=new_cmap, norm=new_norm,
spacing='proportional', ticks=new_bounds, boundaries=new_bounds, format='%1i', **kw)

Note that the mappable kwarg from the colorbar is gone. 请注意,颜色栏中的可映射 kwarg消失了。 Since I was mapping it to an image that does not have values throughout the range in norm , it was rescaling my colorbar to fit the image. 由于我将其映射到在范数范围内没有值的图像,因此它正在重新调整颜色条以适合该图像。 The above now works. 以上工作。 I've changed the colorscheme a bit as well, so don't get confused by the different colours :). 我也更改了colorcheme,所以不要被不同的颜色所迷惑:)。

在此处输入图片说明

(UPDATED: clim ) (更新: clim

Let us make a simpler example: 让我们举一个简单的例子:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors

# create very simple image data
img = np.linspace(-1.9,1.9,100).reshape(1,-1)

# create a very simple color palette
colors = [[1,.5,.5], [1,0,0], [0,1,0], [0,0,1], [0,0,0], [0, .5, .5]]
cm = matplotlib.colors.ListedColormap(colors)
norm = matplotlib.colors.BoundaryNorm([-3,-2,-1,0,1,2,2], cm.N)

# draw the image
f = plt.figure()
ax = f.add_subplot(111)
im = ax.imshow(img, extent=[-1.9,1.9,0,1], cmap=cm, norm=norm)
im.set_clim(-3, 3)

# draw the color bar
plt.colorbar(im)

This gives: 这给出:

在此处输入图片说明

So, it seems that there is very little wrong with your code if you do what GWW suggests in their comment (add the norm=norm kw argument). 因此,如果执行GWW在其注释中建议的操作(添加norm=norm kw参数),似乎代码几乎没有错误。 If you want to display the whole color bar, you need to set the clim for the image. 如果要显示整个颜色条,则需要设置图像的clim

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM