Contour / Quiver Plot Python上的底图叠加

时间:2015-10-21 00:54:15

标签: python matplotlib plot matplotlib-basemap

我正在尝试用我的轮廓/箭袋图绘制底图。我的代码下面没有错误,但图像显示不正确;只显示了箭数。有什么帮助吗?

import netCDF4
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pylab
from mpl_toolkits.basemap import Basemap

ncfile = netCDF4.Dataset('30JUNE2012_0400UTC.cdf', 'r')
dbZ = ncfile.variables['MAXDBZF']
u = ncfile.variables['UNEW']
v = ncfile.variables['VNEW']
print u
print v
print dbZ

data = dbZ[0,0]

data.shape 

print data.shape


z_index = 0  #  z-level you want to plot (0-19)
U = u[0,z_index, :,:] #[time,z,x,y]
V = v[0,z_index, :,:]

map = Basemap(projection = 'merc',llcrnrlat=36,urcrnrlat=40,\
llcrnrlon=-80,urcrnrlon=-74,lat_ts=20,resolution='i')
x = np.arange(0,150)
y = np.arange(0,150)

X,Y = np.meshgrid(x,y)
lon, lat = map(X,Y, inverse = True)

levels = np.arange(5,60,3)
c = plt.contourf(lon,lat,data, levels, cmap='jet')
plt.colorbar()
plt.hold(True)
q=plt.quiver(U,V,width=0.002, scale_units='xy',scale=10)  
qk= plt.quiverkey (q,0.95, 1.02, 20, '20m/s', labelpos='N')
plt.xlim([0,120])

plt.ylim([0,120])


plt.xlabel('X')
plt.ylabel('Y')
plt.title('Reflectivity and Dual-Doppler Winds at 1 KM', fontsize=12)
plt.show()

Here is my data structure:
<type 'netCDF4.Variable'>
float32 UNEW(time, z, y, x)
    missing_value: -32768.0
unlimited dimensions: time
current shape = (1, 20, 150, 150)
filling off

<type 'netCDF4.Variable'>
float32 VNEW(time, z, y, x)
    missing_value: -32768.0
unlimited dimensions: time
current shape = (1, 20, 150, 150)
filling off

<type 'netCDF4.Variable'>
float32 MAXDBZF(time, z, y, x)
    missing_value: -32768.0
unlimited dimensions: time
current shape = (1, 20, 150, 150)
filling off

图片如下: enter image description here

1 个答案:

答案 0 :(得分:1)

您还需要将lon,lat添加到您的箭袋中,否则将以其他轴刻度绘制。 你使用的lat,lon毫无意义(正如@daryl在评论中所说)。 根据您的地图投影,我建议如下:

lonmin = -80
lonmax = -74
latmin = 36
latmax = 40

m = Basemap(projection = 'merc',llcrnrlat=latmin,urcrnrlat=latmax, llcrnrlon=lonmin,urcrnrlon=lonmax,lat_ts=20,resolution='i')

#you should avoid using "map" since its a build-in function.

x = np.arange(lonmin,lonmax,(lonmax-lonmin)/float(U.shape[2]))
y = np.arange(latmin,latmax,(latmax-latmin)/float(U.shape[1]))

X,Y = np.meshgrid(x,y)
lon, lat = m(X,Y)

然后:

c = plt.contourf(lon,lat,data, levels, cmap='jet')
plt.colorbar(c)

q=plt.quiver(lon,lat,U,V,width=0.002, scale_units='xy',scale=10)  
qk= plt.quiverkey (q,0.95, 1.02, 20, '20m/s', labelpos='N')

plt.xlim([lon[0,0],lon[-1,-1]])
plt.ylim([lat[0,0],lat[-1,-1]])

plt.xlabel('X')
plt.ylabel('Y')
plt.title('Reflectivity and Dual-Doppler Winds at 1 KM', fontsize=12)
plt.show()