1. 方法一 pygal
Plotting World Map Using Pygal in Python
import pygal # create a world map
worldmap = pygal.maps.world.SupranationalWorld() # set the title of map
worldmap.title = 'Continents'# adding the continents
worldmap.add('Africa', [('africa')])
worldmap.add('North america', [('north_america')])
worldmap.add('Oceania', [('oceania')])
worldmap.add('South america', [('south_america')])
worldmap.add('Asia', [('asia')])
worldmap.add('Europe', [('europe')])
worldmap.add('Antartica', [('antartica')]) # save into the file
worldmap.render_to_file('abc.svg') print("Success")
结果:
参考资料:
https://www.pygal.org/en/stable/documentation/types/maps/pygal_maps_world.html
2. 方法二 cartopy
官网资料:https://foundations.projectpythia.org/core/cartopy/cartopy.html
参考资料:https://scitools.org.uk/cartopy/docs/latest/
import matplotlib.pyplot as plt
import cartopy.crs as ccrsnplots = 2fig = plt.figure(figsize=(6, 6))for i in range(0, nplots):central_longitude = 0 if i == 0 else 180ax = fig.add_subplot(nplots, 1, i+1,projection=ccrs.PlateCarree(central_longitude=central_longitude))ax.coastlines(resolution='110m')ax.gridlines()
plt.show()
结果:PlateCarree投影(Cartopy的默认投影)
投影方式:Cartopy projection list
- projection=ccrs.Mollweide() 摩尔威德投影(Mollweide Projection)是经线投影成为椭圆曲线的一种等面积伪圆柱投影。
- Robinson:伪圆柱投影。类似于椭圆弧的经线等间距分布,并且以中央子午线为中心向两侧凸出。
具体投影类型可参考:https://www.cnblogs.com/youxiaogang/p/14247184.html
绘制国家边界线:How to draw country borders in Cartopy
代码:
import cartopy.crs as ccrs
import cartopy.feature as cf
ax = plt.axes(projection = ccrs.Mercator())
ax.add_feature(cf.COASTLINE)
ax.add_feature(cf.BORDERS)
# Make figure larger
plt.gcf().set_size_inches(20, 10)
# Save figure as SVG
plt.savefig("Cartopy-Borders.svg")
还可以添加:
ax.add_feature(cfeature.BORDERS, linewidth=0.5, edgecolor='black')
ax.add_feature(cfeature.STATES, linewidth=0.3, edgecolor='brown')
完整代码:
fig = plt.figure(figsize=(10, 5))
projMoll = ccrs.Mollweide(central_longitude=0)
ax = fig.add_subplot(1, 1, 1, projection=projMoll)#Mollweide,Robinson,PlateCarree
lonmin = -175
lonmax = 175
latmin = -55
latmax = 80
ax.set_extent([lonmin, lonmax, latmin, latmax])
ax.add_feature(cfeature.COASTLINE)# 添加海岸线
ax.add_feature(cfeature.BORDERS, linewidth=0.5, edgecolor='blue')#添加国家线
结果:
参考资料:https://foundations.projectpythia.org/core/cartopy/cartopy.html
如何不显示图框:
ax1.axis('off')#关闭子图的坐标轴
ax1.set_aspect('auto')#将子图的纵横比例设置为自动调整
效果如下: