Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib:特定颜色处间距不相等的记号/零_Python_Matplotlib_Plot_Colorbar - Fatal编程技术网

Python matplotlib:特定颜色处间距不相等的记号/零

Python matplotlib:特定颜色处间距不相等的记号/零,python,matplotlib,plot,colorbar,Python,Matplotlib,Plot,Colorbar,我已经尝试解决一个问题有一段时间了,stackoverflow和其他网站都帮不了我 我试图用色条gist_rainbow_r绘制一个范围为-40到36的场(轮廓图),有52个离散步骤。 如您所见,标高的间距不是相等的。我想要的是一个具有等距刻度(所有级别应等距)的颜色条,具有离散化的52种gist_彩虹_r颜色的颜色。这有可能吗?下面是代码snipplet levels = [-40,-30,-20,-15,-12,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2

我已经尝试解决一个问题有一段时间了,stackoverflow和其他网站都帮不了我

我试图用色条gist_rainbow_r绘制一个范围为-40到36的场(轮廓图),有52个离散步骤。 如您所见,标高的间距不是相等的。我想要的是一个具有等距刻度(所有级别应等距)的颜色条,具有离散化的52种gist_彩虹_r颜色的颜色。这有可能吗?下面是代码snipplet

    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()
致意


马丁

事实上,这个解决方案很简单,写在这里有点尴尬。。。抱歉打扰了社区

我们开始吧-这解决了我的问题:

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,我注意到该设置根本不存在),并将“级别”传递给轮廓函数

在这一点上:感谢@Saullo Castro部分回答了这个问题

干杯


马丁

试着把
等级
传给
映射.轮廓()
,就像
映射.轮廓(…,等级=等级)
谢谢你的建议,@Saullo Castro。不幸的是,仍然没有变化。但这是真正答案的一个很好的部分——回答如下!