ホームページ >バックエンド開発 >Python チュートリアル >[Python チュートリアル] 地理視覚化
Matplotlib是Python常用的数据绘制包,其绘图功能强大;而Basemap则是Matplotlib的一个子包,负责地图绘制。本文简单介绍如何利用该程序包绘制风向图。具体操作如下:
导入命令
1)设置工作环境并导入程序包
%cd "F:\\Dropbox\\python" import numpy as np import matplotlib.pyplot as plt import datetime from mpl_toolkits.basemap import Basemap, shiftgrid from netCDF4 import Dataset
3)设定时间并读取数据
yyyy=1993; mm=03; dd=14; hh=00 date = datetime.datetime(yyyy,mm,dd,hh) URLbase="http://nomads.ncdc.noaa.gov/thredds/dodsC/modeldata/cmd_pgbh/" URL=URLbase+"%04i/%04i%02i/%04i%02i%02i/pgbh00.gdas.%04i%02i%02i%02i.grb2" %\ (yyyy,yyyy,mm,yyyy,mm,dd,yyyy,mm,dd,hh) data = Dataset(URL)
4)数据预处理
latitudes = data.variables['lat'][::-1] longitudes = data.variables['lon'][:].tolist() slpin = 0.01*data.variables['Pressure_msl'][:].squeeze() slp[:,0:-1] = slpin[::-1]; slp[:,-1] = slpin[::-1,0]u = np.zeros((uin.shape[0],uin.shape[1]+1),np.float64) u[:,0:-1] = uin[::-1]; u[:,-1] = uin[::-1,0]v = np.zeros((vin.shape[0],vin.shape[1]+1),np.float64)v[:,0:-1] = vin[::-1]; v[:,-1] = vin[::-1,0]longitudes.append(360.); longitudes = np.array(longitudes)lons, lats = np.meshgrid(longitudes,latitudes)
5)设定并绘制图示
m = Basemap(resolution='c',projection='ortho',lat_0=60.,lon_0=-60.)fig1 = plt.figure(figsize=(8,10)) ax = fig1.add_axes([0.1,0.1,0.8,0.8])clevs = np.arange(960,1061,5)x, y = m(lons, lats)parallels = np.arange(-80.,90,20.) meridians = np.arange(0.,360.,20.)CS1 = m.contour(x,y,slp,clevs,linewidths=0.5,colors='k',animated=True) CS2 = m.contourf(x,y,slp,clevs,cmap=plt.cm.RdBu_r,animated=True)ugrid,newlons = shiftgrid(180.,u,longitudes,start=False) vgrid,newlons = shiftgrid(180.,v,longitudes,start=False) uproj,vproj,xx,yy = \ m.transform_vector(ugrid,vgrid,newlons,latitudes,31,31,returnxy=True,masked=True) Q = m.quiver(xx,yy,uproj,vproj,scale=700)qk = plt.quiverkey(Q, 0.1, 0.1, 20, '20 m/s', labelpos='W')m.drawcoastlines(linewidth=1.5) m.drawparallels(parallels) m.drawmeridians(meridians) cb = m.colorbar(CS2,"bottom", size="5%", pad="2%") cb.set_label('hPa') ax.set_title('SLP and Wind Vectors '+str(date)) plt.show()
输出图像如下
以上就是【Python教程】地理可视化的内容,更多相关内容请关注PHP中文网(www.php.cn)!