您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用Python實現中國地圖”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用Python實現中國地圖”吧!
代碼如下:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import cmaps
import shapefile
from matplotlib.path import Path
from matplotlib.patches import PathPatch
ny = grid_z.shape[0]
nx = grid_z.shape[1]
fig=plt.figure(figsize=(16,9))
plt.rc('font',size=15,weight='bold')
ax=fig.add_subplot(111)
plt.title(u'd_rmse_tmax_012_24h',size=25,weight='bold')
mp=Basemap(llcrnrlon=73.,llcrnrlat=15.,urcrnrlon=136.,urcrnrlat=55,projection='cyl')
CHN='G:\\DATA\\MAP\\china-shapefiles-master\\'
mp.readshapefile(CHN+'china','china',drawbounds=True)
sf = shapefile.Reader(CHN+'china_country')
for shape_rec in sf.shapeRecords():
if shape_rec.record[8] == 'CHN':
vertices = []
codes = []
pts = shape_rec.shape.points
prt = list(shape_rec.shape.parts) + [len(pts)]
for i in range(len(prt) - 1):
for j in range(prt[i], prt[i+1]):
vertices.append((pts[j][0], pts[j][1]))
codes += [Path.MOVETO]
codes += [Path.LINETO] * (prt[i+1] - prt[i] -2)
codes += [Path.CLOSEPOLY]
clip = Path(vertices, codes)
clip = PathPatch(clip, transform=ax.transData)
levels=np.linspace(-0.2,0.2,21)
lons, lats = mp.makegrid(nx, ny) # get lat/lons of ny by nx evenly space grid.
x, y = mp(lons, lats)
cf=mp.contourf(x,y,grid_z,levels=levels,extend='both',cmap=cmaps.MPL_bwr)
for contour in cf.collections:
contour.set_clip_path(clip)
cbar=mp.colorbar(cf,location='right',format='%8.2f',size=0.3,ticks=np.linspace(-0.2,0.2,11))
left, bottom, width, height = 0.15, 0.63, 0.25, 0.25
plt.axes([bottom, left, width, height])
mf=Basemap(llcrnrlon=107.,llcrnrlat=2.,urcrnrlon=122.,urcrnrlat=22,projection='cyl')
CHN='G:\\DATA\\MAP\\china-shapefiles-master\\'
mf.readshapefile(CHN+'china','china',drawbounds=True)
CHN='G:\\DATA\\MAP\\china-shapefiles-master\\'
mf.readshapefile(CHN+'china_nine_dotted_line','china_nine_dotted_line',drawbounds=True,linewidth=1.5)
mf.drawcoastlines()
plt.show()
出圖效果如下:
感謝各位的閱讀,以上就是“怎么用Python實現中國地圖”的內容了,經過本文的學習后,相信大家對怎么用Python實現中國地圖這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。