如果 shape 是 .shp 文件的形状,则可以将其提供给 matplotlib.patches.Polygon 并使用 hatch 参数添加一些剖面线 .
p= Polygon(np.array(shape), fill=False, hatch="X")
plt.gca().add_artist(p)
一个完整的例子:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import numpy as np
m = Basemap(llcrnrlon=-10,llcrnrlat=35,urcrnrlon=35,urcrnrlat=60.,
resolution='i', projection='tmerc', lat_0 = 48.9, lon_0 = 15.3)
m.drawcoastlines()
# shape file from
# http://www.naturalearthdata.com/downloads/10m-cultural-vectors/10m-admin-0-countries/
fn = r"ne_10m_admin_0_countries\ne_10m_admin_0_countries"
m.readshapefile(fn, 'shf', drawbounds = False)
# here, 'shf' is the name we later use to access the shapes.
#Madrid
x,y = m([-3.703889],[40.4125])
m.plot(x,y, marker="o", color="k", label="Madrid", ls="")
hatches = ["\\\\","++", "XX"]
countries = ['Spain', 'Ireland', "Belgium"]
hatchdic = dict(zip(countries, hatches))
shapes = {}
for info, shape in zip(m.shf_info, m.shf):
if info['NAME'] in countries:
p= Polygon(np.array(shape), fill=False, hatch=hatchdic[info['NAME']])
shapes.update({info['NAME'] : p})
for country in countries:
plt.gca().add_artist(shapes[country])
handles, labels = plt.gca().get_legend_handles_labels()
handles.extend([shapes[c] for c in countries])
labels.extend(countries)
plt.legend(handles=handles, labels=labels, handleheight=3, handlelength=3, framealpha=1. )
plt.show()