如何根据特定范围的值将 rgb 颜色指定为 contourf
How to specify rgb colors to contourf based on certain range of values
我有以下 rgb 颜色模式和一个形状为 (500, 500) 的数据数组,在每种颜色之后都有应该使用特定颜色的值范围,因此如果数组中的一项低于 5.0,我将使用 255 255 255
颜色,如果另一个项目介于 5.0 和 7.5 之间,则将使用 0 191 255
。
那我该怎么做呢?
# r g b
255 255 255 # <5.0
0 191 255 # 5.0 - 7.5
0 178 238 # 7.5 - 10.0
0 154 205 # 10.0 - 12.5
0 104 139 # 12.5 - 15.0
0 255 127 # 15.0 - 17.5
0 210 102 # 17.5 - 20.0
0 185 85 # 20.0 - 22.5
0 139 69 # 22.5 - 25.0
255 255 0 # 25.0 - 27.5
205 205 0 # 27.5 - 30.0
215 180 0 # 30.0 - 32.5
200 135 0 # 32.5 - 35.0
225 60 0 # 35.0 - 37.5
150 0 0 # 37.5 - 40.0
191 62 255 # 40.0 - 42.5
178 58 238 # 42.5 - 45.0
154 50 205 # 45.0 - 47.5
104 34 139 # 47.5 - 50.0
255 255 255 # > 50.0
对于离散颜色,使用 ListedColormap
and a BoundaryNorm
:
是有意义的
import numpy as np
import matplotlib.colors
rgb = [[255, 255, 255], # <5.0
[0, 191, 255], # 5.0 - 7.5
[0, 178, 238]] # 7.5 - 10.0
rgb=np.array(rgb)/255.
val = np.array([0,5,7.5,10])
cmap = matplotlib.colors.ListedColormap(rgb,"")
norm = matplotlib.colors.BoundaryNorm(val,len(val)-1)
#test:
print(np.array(cmap(norm(6)))*255)
# prints [ 0. 191. 255. 255.] as expected
我有以下 rgb 颜色模式和一个形状为 (500, 500) 的数据数组,在每种颜色之后都有应该使用特定颜色的值范围,因此如果数组中的一项低于 5.0,我将使用 255 255 255
颜色,如果另一个项目介于 5.0 和 7.5 之间,则将使用 0 191 255
。
那我该怎么做呢?
# r g b
255 255 255 # <5.0
0 191 255 # 5.0 - 7.5
0 178 238 # 7.5 - 10.0
0 154 205 # 10.0 - 12.5
0 104 139 # 12.5 - 15.0
0 255 127 # 15.0 - 17.5
0 210 102 # 17.5 - 20.0
0 185 85 # 20.0 - 22.5
0 139 69 # 22.5 - 25.0
255 255 0 # 25.0 - 27.5
205 205 0 # 27.5 - 30.0
215 180 0 # 30.0 - 32.5
200 135 0 # 32.5 - 35.0
225 60 0 # 35.0 - 37.5
150 0 0 # 37.5 - 40.0
191 62 255 # 40.0 - 42.5
178 58 238 # 42.5 - 45.0
154 50 205 # 45.0 - 47.5
104 34 139 # 47.5 - 50.0
255 255 255 # > 50.0
对于离散颜色,使用 ListedColormap
and a BoundaryNorm
:
import numpy as np
import matplotlib.colors
rgb = [[255, 255, 255], # <5.0
[0, 191, 255], # 5.0 - 7.5
[0, 178, 238]] # 7.5 - 10.0
rgb=np.array(rgb)/255.
val = np.array([0,5,7.5,10])
cmap = matplotlib.colors.ListedColormap(rgb,"")
norm = matplotlib.colors.BoundaryNorm(val,len(val)-1)
#test:
print(np.array(cmap(norm(6)))*255)
# prints [ 0. 191. 255. 255.] as expected