无法在Basemap上绘制pcolormesh

2024-04-29 01:59:06 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的程序中,有一个绘图功能 基本地图上的pcolormesh。此函数在循环中调用,如果我调用此函数一次,则可以在Basemap上绘制pcolormesh,但如果我多次调用此函数,则只有第一个循环才能在Basemap和铰孔迭代上绘制pcolormesh。只有底图是空的P颜色如下是密码。在

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

Tags: nonenormfieldifplotformattercmapset