首页 文章

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

提问于
浏览
1

我正在尝试在代表美国各州的特定多边形上添加阴影线 . 每个多边形的填充与该特定县的杂货店数量相关 . 孵化将代表每个县的成人糖尿病发病率的5个水平(从最高到最低的范围) .

我遇到了各种可能的实现方法,但每次我离开都有点难过 .

  • 我在这里看到有人使用 Descartes 包绘制单个多边形的帖子,但是像其他帖子一样,我不确定是否必须使用多边形的 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 上孵化单个多边形?

1 回答

  • 1

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

    enter image description here

相关问题