如何在基图中的单个多边形上添加点划线或斜线?
我正在尝试在美国某个州的特定多边形上添加斜线,这些多边形代表该州的县。每个多边形的填充颜色与该县的杂货店数量有关。斜线则表示每个县成年人糖尿病发病率的五个等级(从最高到最低)。
我遇到过各种可能的实现方法,但每次都让我感到困惑。
- 我看到这里有一个帖子,里面有人使用了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, ...]
- 当我遇到等高线图时,我的问题是它是针对整个图的,我无法限制特定多边形的经纬度范围,因为没有足够的值。我觉得我在裁剪方面也有同样的问题。
如这里所示:使用pcolormesh在Basemap中填充区域
我应该提到,我的绘图数据是一个形状文件,并且我有关于2010年成年人糖尿病发病率、每个县的杂货店数量以及其他变量的列。
有没有办法在basemap
上对单个多边形进行斜线填充?
相关问题:
- 暂无相关问题
1 个回答
2
如果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()