[英]pcolormesh () and contourf() do not work
尊敬的专家,我回到了大约两个月前提出的问题,从那以后我一直在努力。 这涉及底图上轮廓的叠加。 我看过很多例子,例如这里的例子: http : //nbviewer.ipython.org/github/Unidata/tds-python-workshop/blob/master/matplotlib.ipynb我以前的帖子,在这里: python中不规则网格上覆盖地图的轮廓 。 准备数据后,以下是绘制方法:
# 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()
抱歉,我无法发布情节示例,但是:
pcolor
, pcolormesh
和contourf
给出了一个没有填充轮廓但带有contourf
的贴图 我很困惑,因为这是从上面引用的链接示例复制粘贴的示例。
关于该问题的可能原因的任何提示将不胜感激Zilore Mumba
与使用matplotlib.pyplot相比,您需要使用底图来绘制轮廓。 请参阅我的示例以获取一些代码。
#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()
不知道您的数据看起来如何,很难回答您的问题,但是我还是会尝试的。 您可能希望例如使用直方图对数据进行网格化,然后对结果进行轮廓处理。
例如,如果您对绘制具有坐标( x
, y
)和要用于颜色的第三个属性( z
)的点的2D轮廓感兴趣,则可以尝试一下
from numpy import *
H=histogram2d(x,y,weights=z)
contourf(H[0].T,origin='lower')
但是,就像我说的那样,如果您不提供有关数据的详细信息,就很难理解您要查找的内容。 请参阅matplotlib指南以获取更多示例http://matplotlib.org/examples/pylab_examples/contourf_demo.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.