我有一组数据,范围从-0.5到2(例如)。我只想为[-0.5,1]内的数据着色。我做了类似的事情:
from matplotlib import colors as mpl_colors
color_map = plt.cm.jet
color_map.set_bad('gray', 1.0)
norm_color = mpl_colors.Normalize(vmin=-0.5, vmax=1, clip=False)
nb_colors = 40
map_frame.contourf(x_mesh, y_mesh, data, nb_colors, cmap=color_map, norm=norm_color, extend='both')
我希望色条有40个彩色步幅,范围从-0.5到1。
相反,我得到以下图片:
颜色栏不会像预期的那样停在1,就像在这里一样:http://matplotlib.org/examples/pylab_examples/contourf_demo.html
你知道为什么吗?
答案 0 :(得分:0)
这是我的解决方案。
我没有您的数据,所以我选择我的数据作为示例。
模型模拟2米处的温度显示如下:
我们可以看到温度范围从268.5到280.5
为了实现你的类比尝试,我在这里写了一些代码:
1 A (as 1 and A never appear then keep the obs)
1 B (as 1 appear already then delete)
1 C (as 1 appear then delete)
2 A (as A appear then delete)
2 B (as 2 and B never appear then keep the obs)
2 C (as 2 appear then delete)
3 A (as A appear then delete)
3 B (as B appear then delete)
3 C (as 3 and C never appear then keep the obs)
### 1. Mask the location which temperature not in the range of [265,278]
t_mask_1 = np.ma.masked_greater(t,278)
t_mask_2 = np.ma.masked_less(t_mask_1,265)
### 2. Generate the colormap with 40 colored strides
cs=plt.cm.jet(np.arange(40)/40.)
### 3. Plot the contourf figure
cf = map.pcolormesh(x, y ,t,40, cmap= plt.cm.jet,alpha = 0.8)
希望它会有所帮助!
我在这里也发现了一个问题。通过屏蔽超出用户定义范围的数据,cMap = plt.cm.get_cmap("jet",lut=40)
pc = map.pcolormesh(x, y ,t,cmap= cMap,alpha = 0.8)
颜色条可以很好地适应数据范围。但是contourf
的颜色条在底部似乎是错误的,蓝色打击小于270.