Python Numpy阵列到天气图

Python Numpy阵列到天气图,python,arrays,numpy,matplotlib,Python,Arrays,Numpy,Matplotlib,我有一些numpy数组数据,如下所示 Layer 1 Layer 2 LATITUDE LONGITUDE 1.7964935 3.592987 24.813797 -88.901495 1.6611315 3.322263 24.6915 -87.84013 1.4888205 2.977641 24.694245 -87.868856 4.7826225 9.565245 29.965457 -87.5655

我有一些
numpy
数组数据,如下所示

 Layer 1     Layer 2    LATITUDE     LONGITUDE
1.7964935   3.592987    24.813797   -88.901495
1.6611315   3.322263    24.6915     -87.84013
1.4888205   2.977641    24.694245   -87.868856
4.7826225   9.565245    29.965457   -87.565512
2.303408    4.606816    26.119643   -88.410623
3.7254165   7.450833    29.968328   -87.596143
我想把这个
numpy
数组转换成天气图。我已经通过matplotlib准备好了底图

import matplotlib.pyplot as plt
plt.switch_backend('agg')
from mpl_toolkits.basemap import Basemap
import numpy as np


#Lambert Conformal map of USA lower 48 states
plt.figure(figsize=(125,100))
m=Basemap ( llcrnrlon = -119 ,
llcrnrlat =22 ,
urcrnrlon = -64 ,
urcrnrlat =49 ,
projection = 'lcc' ,
lat_1 =33 ,
lat_2 =45 ,
lon_0 = -95 ,
resolution = 'h' ,
area_thresh =10000)
m.drawcoastlines(linewidth = 3, color = 'white')
m.drawcountries(linewidth =3, color = 'white')
m.drawstates(linewidth = 2, color = 'white')
m.drawmapboundary(fill_color='black')
plt.title('MAP', fontsize=200)
plt.suptitle('TEMPERATURES', fontsize=50) # Add the text/suptitle to figure

plt.show()
plt.savefig('pic')
“我想将我的numpy数据作为像这样的光栅化映射导入此底图”

他们使用的代码如下。注意,我的数据不是NetCdfile。我有一个
numpy
数组

from mpl_toolkits.basemap import Basemap, cm
#requires netcdf4-python (netcdf4-python.googlecode.com)
from netCDF4 import Dataset as NetCDFFile
import numpy as np
import matplotlib.pyplot as plt

#plot rainfall from NWS using special precipitation
#colormap used by the NWS, and included in basemap.

nc = NetCDFFile('../../../examples/nws_precip_conus_20061222.nc')
#data from http://water.weather.gov/precip/
prcpvar = nc.variables['amountofprecip']
data = 0.01*prcpvar[:]
latcorners = nc.variables['lat'][:]
loncorners = -nc.variables['lon'][:]
lon_0 = -nc.variables['true_lon'].getValue()
lat_0 = nc.variables['true_lat'].getValue()
#create figure and axes instances
fig = plt.figure(figsize=(8,8))
ax = fig.add_axes([0.1,0.1,0.8,0.8])
#create polar stereographic Basemap instance.
m =Basemap(projection='stere',lon_0=lon_0,lat_0=90.,lat_ts=lat_0,\
            llcrnrlat=latcorners[0],urcrnrlat=latcorners[2],\
            llcrnrlon=loncorners[0],urcrnrlon=loncorners[2],\
             rsphere=6371200.,resolution='l',area_thresh=10000)
#draw coastlines, state and country boundaries, edge of map.
m.drawcoastlines()
m.drawstates()
m.drawcountries()
#draw parallels.
parallels = np.arange(0.,90,10.)
m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)
#draw meridians
meridians = np.arange(180.,360.,10.)
m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10)
ny = data.shape[0]; nx = data.shape[1]
lons, lats = m.makegrid(nx, ny) # get lat/lons of ny by nx evenly space grid.
x, y = m(lons, lats) # compute map proj coordinates.
#draw filled contours.
clevs = [0,1,2.5,5,7.5,10,15,20,30,40,50,70,100,150,200,250,300,400,500,600,750]
cs = m.contourf(x,y,data,clevs,cmap=cm.s3pcpn)
#add colorbar.
cbar = m.colorbar(cs,location='bottom',pad="5%")
cbar.set_label('mm')
#add title
plt.title(prcpvar.long_name+' for period ending '+prcpvar.dateofdata)
plt.show()

有什么想法吗?

使用netcdf文件和numpy阵列之间没有太大区别。当然,numpy阵列更直接适用,这使它更容易实现。这里的主要区别在于,在您引用的代码中,它们的数据位于常规网格上,但您的数据似乎完全是任意的。您可以使用这些数据绘制一个
tricontourf
,或者在绘制等高线之前,您需要先在规则网格上插值这些数据。@ImportanceOfBeingErnest使用tricontoutf函数是个好主意。由于我已将numpy数组导出为output=np.compress(map(pointInBox,output),output,axis=0),我如何将其导入matplot basemap并绘制此数据?basemap发挥作用的唯一地方是
x,y=m(lons,lats)
。但我担心Basemap没有tricontourf功能;相反,您需要使用该函数。@ImportanceOfBeingErnest所以如果是这样的:np.savetxt(f,Data.npy)然后是tourtf(x,y,Data.npy,*args,**kwargs)?它将是
tourtf(x,y,z,tri=True)
其中x,y,z是一些numpy数组。