這篇文章主要講解了“怎么用Python實現(xiàn)中國地圖”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用Python實現(xiàn)中國地圖”吧!
長葛網(wǎng)站建設公司創(chuàng)新互聯(lián),長葛網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為長葛上千提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的長葛做網(wǎng)站的公司定做!
代碼如下:
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實現(xiàn)中國地圖”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對怎么用Python實現(xiàn)中國地圖這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!