简体   繁体   中英

pcolormesh () and contourf() do not work

Esteemed experts, am back with a problem I presented about two months ago, I have been working on it since with no success. This concerns superposition of contours on a basemap. I have looked at numerous examples on this, eg the example here: http://nbviewer.ipython.org/github/Unidata/tds-python-workshop/blob/master/matplotlib.ipynb A sample of the data is on one of my previous posts, here: Contours with map overlay on irregular grid in python . After preparing the data, here are plotting methods:

# Setting the plot size and text
fig = plt.figure(figsize=(10,8))

lev = [15, 20, 25, 30, 35, 40,45]
norm1 = colors.BoundaryNorm(lev, 256)

# Draw filled contours

# 1. pcolor does not show the filled contours 
#cs = plt.pcolor(x,y,zi, cmap = cm.jet, norm = norm1)

# 2. pcolormesh does not show the filled contours
#cs = plt.pcolormesh(x,y,zi, shading = "flat", cmap=cmap)

# 3. contourf does not show the filled contours
#cs = plt.contourf(xi, yi, zi) #, levels=np.linspace(zi.min(),zi.max(),5))
cs = plt.contourf(xi, yi, zi, cmap = cm.jet, levels = lev, norm = norm1)

# 4. Draw line contours with contour()
#cs = m.contour(x,y,zi,linewidths=1.2)              # This works

plt.scatter(data.Lon, data.Lat, c=data.Z, s=100,
            vmin=zi.min(), vmax=zi.max())           # Does not work at all

# Color bar
#cbar = m.colorbar(fig,location='right',pad="10%")
fig.colorbar(cs)

# Plot a title
plt.figtext(.5,.05,'Figure 1. Mean Rainfall Onset Dates',fontsize=12,ha='center')

plt.show()

Sorry I am not able to post the plot examples, but:

  • pcolor , pcolormesh and contourf above all give a map without any filled contours but with a colorbar
  • the above plots without the map object give filled contours including scatter plot (without map background)
  • contour gives the map with contour lines superposed:

I am baffled because this is an example copy-pasted from the example in the link quoted above.

Any hint as to a possible cause of the problem would be appreciated Zilore Mumba

you need to use the basemap to plot the contours vs using matplotlib.pyplot. see my example for some of my code.

#Set basemap and grid
px,py=n.meshgrid(x,y)

m=Basemap(projection='merc',llcrnrlat=20,urcrnrlat=55,
   llcrnrlon=230,urcrnrlon=305,resolution='l')

X,Y=m(px,py)


#Draw Latitude Lines
#labels[left,right,top,bottom]  1=True 0=False
parallels = n.arange(0.,90,10.)
m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10,linewidth=0.)

# Draw Longitude Lines
#labels[left,right,top,bottom]  1=True 0=False
meridians = n.arange(180.,360.,10.)
m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10,linewidth=0)

#Draw Map
m.drawcoastlines()
m.drawcountries()
m.drawstates()
m.fillcontinents(color='grey',alpha=0.1,lake_color='aqua')

#Plot Contour lines and fill
levels=[5.0,5.1,5.2,5.3,5.4,5.6,5.7,5.8,5.9,6.0]
cs=m.contourf(px,py,thickness,levels,cmap=p.cm.RdBu,latlon=True,extend='both')
cs2=m.contour(px,py,thickness,levels,latlon=True,colors='k')

#Plot Streamlines
m.streamplot(px,py,U,V,latlon=True,color='k')

#Add Colorbar
cbar = p.colorbar(cs)
cbar.add_lines(cs2)
cbar.ax.set_ylabel('1000 hPa - 500 hPa Thickness (km)')

#Title
p.title('Geostrophic Winds with Geopotential Thickness')


p.show()

我的身材

Without knowing how your data look like it's a bit difficult to answer your question, but I'll try anyway. You might want to grid your data, for example, with an histogram, then contour the results.

For example, if you're interested in plotting 2D contours of points that have coordinates ( x , y ) and a third property ( z ) you want to use for the colors, you might give this a try

from numpy import *

H=histogram2d(x,y,weights=z)

contourf(H[0].T,origin='lower')

But, like I said, it's hard to understand what you're looking for if you're not giving details about your data. Have a look at the matplotlib guide for more examples http://matplotlib.org/examples/pylab_examples/contourf_demo.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM