Gra*_*den 3 python plot matplotlib colorbar
我一直在尝试解决一个问题,但无论是 stackoverflow 还是其他网站都无法帮助我。
我正在尝试使用具有 52 个离散步长的颜色条 gist_rainbow_r 绘制范围从 -40 到 36 的字段(轮廓)。如您所见,这些级别不是等距分布的。我想要的是一个带有等距刻度(所有级别应该等距)的颜色条,具有离散化的 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()
Run Code Online (Sandbox Code Playgroud)
此致
马丁
其实,解决方法很简单,写在这里有点尴尬……抱歉打扰了社区!
我们开始 - 这解决了我的问题:
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()
Run Code Online (Sandbox Code Playgroud)
所以,要清楚:将“间距”设置为“统一”(而不是相等 - 通过查看 colorbar.py 我注意到这个设置根本不存在)并将“级别”传递给轮廓函数。
在这一点上:感谢@Saullo Castro 部分回答了这个问题!
干杯
马丁
| 归档时间: |
|
| 查看次数: |
2058 次 |
| 最近记录: |