在R中是否有任何python等效的get_map函数?

时间:2017-05-22 06:42:29

标签: matplotlib plot ggplot2

我需要在不指定llcrnrlat,urcrnrlat,llcrnrlon和urcrnrlon的情况下绘制一个国家的地图(根据要求可能是任何国家)。

1 个答案:

答案 0 :(得分:0)

您可以使用底图而不指定地图的投影和坐标来读取shapefile。一个可以例如从here获取国家/地区的形状。

瑞士地图示例:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import numpy as np

m = Basemap()

fn = r"ne_10m_admin_0_countries\ne_10m_admin_0_countries"
m.readshapefile(fn, 'shf', drawbounds = False)

def draw_country(country, ax=None,**kwargs):
    if ax == None: ax=plt.gca()
    for info, shape in zip(m.shf_info, m.shf):
        if info['NAME'] == country:
            s = np.array(shape)
            ax.plot(s[:,0], s[:,1], alpha=0)
            p= Polygon(s, **kwargs)
            ax.add_artist(p)


fig, ax = plt.subplots()
ax.set_aspect("equal")
draw_country("Switzerland",fill=True, facecolor= "crimson", edgecolor='none', alpha=0.7)
# try with other contries like 'Ireland', "Venezuela" etc
plt.show()

enter image description here

当然,缺点是当没有指定投影时,国家形状可能不是我们习惯的常规地图。

要在带有图例的地图上绘制国家/地区,请参阅例如this question