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)!
本文原創發布php中文網,轉載請注明出處,感謝您的尊重!
總結
以上是生活随笔為你收集整理的python地理_【Python教程】地理可视化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python数据清洗 - 洗什么?怎么洗
- 下一篇: keepass2Android密码,Ke