Python 无法在底图上绘制pcolormesh

Python 无法在底图上绘制pcolormesh,python,plot,matplotlib-basemap,Python,Plot,Matplotlib Basemap,在我的程序中,有一个用于绘图的函数 基本地图上的pcolormesh。此函数在循环中调用,如果我调用此函数一次,则可以在Basemap上绘制pcolormesh,但如果我多次调用此函数,则只有第一个循环才能在Basemap上绘制pcolormesh和扩孔迭代。它只打印带有空pcolormesh的底图。下面是代码 cbar_opt = "vertical" fig = plt.figure() axes = fig.add_subplot(111) if(ptype == 1): # Co

在我的程序中,有一个用于绘图的函数 基本地图上的pcolormesh。此函数在循环中调用,如果我调用此函数一次,则可以在Basemap上绘制pcolormesh,但如果我多次调用此函数,则只有第一个循环才能在Basemap上绘制pcolormesh和扩孔迭代。它只打印带有空pcolormesh的底图。下面是代码

cbar_opt = "vertical"

fig = plt.figure()
axes = fig.add_subplot(111)



if(ptype == 1): # Contour plot
    plot = axes.contourf(xs,ys,field,levels=clevels,cmap=cmap)
elif(ptype == 2): # pcolor plot
    # Mask values below lower bounds
    field = N.ma.masked_where(field < clevels[0],field)
    try:
        cmapname = cmap.name[-9:]
    except:
        cmapname = None
    if cmapname == "_discrete":
        norm = matplotlib.colors.Normalize(vmin=clevels[0], vmax=clevels[-1])
    else:
        norm = matplotlib.colors.BoundaryNorm(clevels,cmap.N)

    plot = axes.pcolormesh(x,y,field,vmin=clevels[0],vmax=clevels[-1],cmap=cmap,norm=norm,edgecolors='None',antialiased=False,rasterized=True)instead

if len(clevels) > 20:
    cintvs = tickLocations(clevels, cbar_opt, maxsize=20)
    clvllocator = MultipleLocator(base=cintvs[1] - cintvs[0])
else:
    cintvs = clevels
    clvllocator = FixedLocator(cintvs)

divider = make_axes_locatable(axes)
if(cbar_opt == "vertical"):
     cax = divider.append_axes("right",size="5%",pad=0.05)
     plt.colorbar(plot,orientation='vertical',ticks=clvllocator,cax=cax)
else:
     cax = divider.append_axes("top",size="5%",pad=0.05)
     cax.xaxis.set_ticks_position('top')
     plt.colorbar(plot,orientation='horizontal',ticks=clvllocator,cax=cax)
if(ovrfieldopt):
     plotovr = axes.contour(xs,ys,ovrfield,levels=ovrfieldlvl,colors='k',lw=2,alpha=0.25)
if(gis_info != None and bgmap != None):
    gis_x, gis_y = bgmap(*gis_info[1:][::-1])
    axes.plot(gis_x, gis_y,'ko')

formatter = FuncFormatter(mtokm)
axes.xaxis.set_major_formatter(formatter)
axes.yaxis.set_major_formatter(formatter)
axes.xaxis.set_major_locator(MultipleLocator(base=xtickintv)) # Configurable in input file 
axes.yaxis.set_major_locator(MultipleLocator(base=ytickintv)) # Configurable in input file (TAS 9-6-13)
axes.set_xlabel('km')
axes.set_ylabel('km')

if(xtickintv == ytickintv):     
    axes.set_aspect('equal')
if(ovrmap and bgmap != None): 
    bgmap.drawcoastlines(ax=axes)
    bgmap.drawcountries(ax=axes)

# Set the axes limits after the basemap calls, because basemap resets the axes limits 
axes.set_xlim(xlim[0],xlim[1])
axes.set_ylim(ylim[0],ylim[1])


return fig, axes
cbar\u opt=“垂直”
图=plt.图()
轴=图添加子批次(111)
如果(ptype==1):#等高线图
曲线图=轴。轮廓图(X、Y、字段、级别=克里夫勒,cmap=cmap)
elif(ptype==2):#pcolor图
#低于下限的掩码值
field=N.ma.屏蔽,其中(field20:
cintvs=滴答声位置(克利夫勒、cbar_opt、maxsize=20)
clvllocator=多路扩频器(基=cintvs[1]-cintvs[0])
其他:
cintvs=克里夫斯
clvllocator=固定定位器(cintvs)
分隔器=使轴可定位(轴)
如果(cbar_opt==“垂直”):
cax=分隔器。附加_轴(“右”,尺寸=“5%”,焊盘=0.05)
plt.colorbar(绘图,方向=垂直,刻度=clvllocator,cax=cax)
其他:
cax=分隔器。附加_轴(“顶部”,size=“5%”,pad=0.05)
cax.xaxis.set_ticks_位置('top'))
plt.colorbar(绘图,方向=水平,刻度=clvllocator,cax=cax)
如果(ovrfieldopt):
plotovr=轴轮廓(xs、ys、OVRFELD、levels=ovrfieldlvl、colors='k',lw=2、alpha=0.25)
如果(gis_信息!=无和bgmap!=无):
gis_x,gis_y=bgmap(*gis_信息[1:][:-1])
坐标轴绘图(gis_x,gis_y,'ko')
格式化程序=函数格式化程序(mtokm)
axes.xaxis.set\u major\u格式化程序(格式化程序)
axes.yaxis.set\u major\u格式化程序(格式化程序)
axes.xaxis.set_major_locator(MultipleLocator(base=xtickintv))#可在输入文件中配置
axes.yaxis.set_major_locator(MultipleLocator(base=ytickintv))#可在输入文件(TAS 9-6-13)中配置
轴设置标签('km')
轴设置标签('km')
如果(xtickintv==ytickintv):
轴。设置方向(“相等”)
如果(ovrmap和bgmap!=无):
bgmap.绘制海岸线(ax=轴)
bgmap.drawcountries(ax=轴)
#在basemap调用后设置轴限制,因为basemap会重置轴限制
axes.set_xlim(xlim[0],xlim[1])
axes.set_ylim(ylim[0],ylim[1])
返回图,轴

感谢dilberted所做的更改。您应该给出一些重现错误的代码。