无法在Basemap上绘制pcolormesh

时间:2015-10-05 02:19:28

标签: python plot matplotlib-basemap

在我的程序中,有一个绘图功能 在底图上的pcolormesh。这个函数在一个循环中被调用,如果我调用这个函数,那么我能够在Basemap上绘制pcolormesh,但如果我多次调用这个函数,那么只有第一个循环就可以在底图和扩孔迭代上绘制pcolormesh 。它只绘制带有空pcolormesh的底图.Below是代码。

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

0 个答案:

没有答案