我一直在尝试解决问题,而stackoverflow和其他网站都无法帮助我。
我试图绘制一个范围从-40到36的字段(contourf),其中colorbar gist_rainbow_r有52个不连续的步骤。 如您所见,水平不是等间隔的。我想要的是具有相等间距刻度的颜色条(所有级别应该等间距),具有离散化的52 gist_rainbow_r颜色的颜色。这是可能吗?下面是代码片段。
levels = [-40,-30,-20,-15,-12,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36]
# define the colormap
cmap = cm.get_cmap('gist_rainbow_r',52)
# define the bins and normalize
norm = mpl.colors.BoundaryNorm(levels, cmap.N)
contourplot = map.contourf(xpoints,ypoints,fieldtoplot,range(-40,36),cmap=cmap,norm=norm,spacing='regular')
ax = plt.gca() # Gets the current axes
divider = make_axes_locatable(ax) # Lets us move axes around
cax = divider.append_axes("bottom", size="2%",pad=-0.02,axes_class=maxes.Axes) #Adds an axis for the colorbar
F.add_axes(cax) # Adds the new axis to the figure as the current working axis
bar = plt.colorbar(contourplot,cax=cax,orientation='horizontal',spacing='regular',extend='both',extendfrac='auto',extendrect='True',format='%1i',ticks=levels, boundaries=levels) # Plots colorbar in new axis
bar.update_ticks()
祝你好运
马丁
答案 0 :(得分:4)
实际上,解决方案非常简单,在这里写它有点尴尬......对不起打扰社区!
我们走了 - 这解决了我的问题:
levels = [-40,-30,-20,-15,-12,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36]
# define the colormap
cmap = cm.get_cmap('gist_rainbow_r',52)
# define the bins and normalize
norm = mpl.colors.BoundaryNorm(levels, cmap.N)
contourplot = map.contourf(xpoints,ypoints,fieldtoplot,range(-40,36),cmap=cmap,norm=norm,spacing='uniform', levels=levels)
ax = plt.gca() # Gets the current axes
divider = make_axes_locatable(ax) # Lets us move axes around
cax = divider.append_axes("bottom", size="2%",pad=-0.02,axes_class=maxes.Axes) #Adds an axis for the colorbar
F.add_axes(cax) # Adds the new axis to the figure as the current working axis
bar = plt.colorbar(contourplot,cax=cax,orientation='horizontal',spacing='uniform',extend='both',extendfrac='auto',extendrect='True',format='%1i',ticks=levels, boundaries=levels) # Plots colorbar in new axis
bar.update_ticks()
因此,要明确:将“间距”设置为“均匀”(并且不等于 - 通过查看colorbar.py我注意到此设置根本不存在)并将“级别”传递给contourf函数。
此时:感谢@Saullo Castro部分回答这个问题!
干杯
马丁