您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用Python實(shí)現(xiàn)中國地圖”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用Python實(shí)現(xiàn)中國地圖”吧!
代碼如下:
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實(shí)現(xiàn)中國地圖”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么用Python實(shí)現(xiàn)中國地圖這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。