[Python Tutorial] Geographical Visualization

Source: Internet
Author: User
Matplotlib is a commonly used data drawing package in Python. it has powerful plotting functions, while Basemap is a sub-package of Matplotlib, which is used to draw maps. This section describes how to use this package to draw a wind direction chart. The specific operations are as follows: Matplotlib is a commonly used data drawing package in Python, and its drawing function is powerful. Basemap is a sub-package of Matplotlib, which is used for map drawing. This section describes how to use this package to draw a wind direction chart. The procedure is as follows:

Import command

1) set the working environment and import the package

%cd "F:\\Dropbox\\python"import numpy as npimport matplotlib.pyplot as pltimport datetimefrom mpl_toolkits.basemap import Basemap, shiftgridfrom netCDF4 import Dataset

3) set the time and read data

yyyy=1993; mm=03; dd=14; hh=00date = 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) Data preprocessing

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) set and draw an illustration

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()

The output image is as follows:

The above is the content of geographic visualization in the [Python Tutorial]. For more information, see The PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.