Plotting World Map in Python

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 ccrs

nplots = 2

fig = plt.figure(figsize=(6, 6))

for i in range(0, nplots):
    central_longitude = 0 if i == 0 else 180
    ax = 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')#将子图的纵横比例设置为自动调整

效果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值