如何为底图中的单个多边形添加点画或影线?

How to add stippling or hatching to individual polygons in a basemap?

我正在尝试在代表美国某个州的县的特定多边形上添加阴影线。每个多边形都根据该特定县的杂货店数量进行填充。阴影线将代表每个县成人糖尿病发病率的 5 个水平(从最高到最低的范围)。

我遇到过各种可能的实现方式,但每次离开时都感到困惑。

-我在这里看到了 post,有人使用 Descartes 包绘制了一个单独的多边形,但就像我看到的其他 post 一样,我我不确定是否必须对多边形使用 GeoJSON 坐标?我通过 pyshp 包获得了每个多边形的坐标,这些坐标是否正确?我觉得他们不是..

Descartes 示例:https://gis.stackexchange.com/questions/197945/geopandas-polygon-to-matplotlib-patches-polygon-conversion

来自 pyshp 包的坐标示例:

from area import area
sf = shapefile.Reader("county_az.shp")
Shapes = sf.shapes()
bbox = Shapes[3].bbox

#1
[-12296461.118197802, 3675955.2075050175,  -12139158.....]

#2
[-12618534.046562605, 4063552.9669038206, -12328686.313377801, ....]

#3
[-12436701.142463798, 3893144.9847336784, -12245216.013980595, ...]

-当我遇到等高线图时,我的问题是它适用于整个图,我无法限制特定多边形的纬度和经度范围,因为没有足够的值。我相信我也有同样的剪辑问题。

如下所示:Hatch area using pcolormesh in Basemap

我应该注意,我的绘图数据是一个 shapefile,我有关于 2010 年成人糖尿病发病率、每个县的杂货店以及其他变量的列。

有没有办法在 basemap 上对单个多边形进行孵化?

如果 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()