这篇文章主要讲解了“怎么用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。