gpt4 book ai didi

python - 自定义颜色条

转载 作者:行者123 更新时间:2023-11-30 23:00:26 25 4
gpt4 key购买 nike

我正在尝试创建自定义颜色条,但色阶看起来与我定义的完全不同 - enter image description here

颜色应该从棕色 -> 白色 -> 蓝色,应该有 9 个类别。

我的代码附在下面:

plot = np.clip(plot,40,250)

bounds = [0, 40, 60, 80, 100, 125, 150, 200, 250, 300]
rgblist = [(51,25,0), (102,51,0), (153,76,0), (239,159,80), (255, 255, 255),
(153,204,255), (51,153,255), (0,102,204), (2,2,128)]
clist = [[c/255 for c in rgb] for rgb in rgblist]
cmap = colors.ListedColormap(clist)
norm = colors.BoundaryNorm(bounds, cmap.N)


cs = m.contourf(X,Y,plot,bounds, cmap=cmap, norm=norm)
cbar = m.colorbar(cs, ticks=[20,50,70,90,112.5,137.5,175,225,275])
cbar.set_ticklabels(['<40','40-60', '60-80', '80-100', '100-125', '125-150', '150-200', '200-250', '>250'])

plt.show()

最佳答案

我猜你正在使用Python 2.x。在这种情况下,您的线路

clist = [[c/255 for  c in rgb] for rgb in rgblist]

正在执行整数除法,即结果存储为整数,所有小数信息都会丢失。在您的情况下,这会导致除 255/255 之外的所有除法结果为 0

要更改此设置,您可以通过添加小数点来除以 float :

clist = [[c/255. for  c in rgb] for rgb in rgblist]

或者您可以在除法之前导入 Python 3 除法行为

from __future__ import division

关于python - 自定义颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35276787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com