python - 使用 basemap 中的颜色填充 shp 文件中的多边形

标签 python matplotlib-basemap polygons

我创建了一个点图,并使用 python basemap 在其上覆盖了县边界。我有另一个 shapefile,它基于 IUCN 数据的物种分布图(species_13143)。我已将该形状文件叠加到之前的 map 上。效果很好。但不幸的是,它没有填充与其关联的多边形。我想根据这张 map 使用颜色来填充这些多边形(我不介意颜色;单色就可以了)

this map

我在 StackOverflow 中发现了类似的问题。但这些对我来说都不起作用。代码和相关图像附在这里.. enter image description here

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.figure(figsize=(5,10))
map = Basemap(projection = 'merc',  resolution = 'h',area_thresh = 0.5, llcrnrlon=79.2643, llcrnrlat=5.3135, urcrnrlon=82.0658, urcrnrlat=9.951)
map.drawcoastlines(linewidth=0.5)
map.drawcountries(linewidth=0.5)
map.fillcontinents(alpha=0.5)
map.drawmapboundary()
map.drawmeridians(np.arange(0, 360, 0.5), labels=[False, False, False, True], linewidth=0.1,)
map.drawparallels(np.arange(-90, 90, 0.5), labels=[False, True, False, False], linewidth=0.1)
#Read district boundaries.
sh_info=map.readshapefile('C:\\Users\\Tharindu\\Downloads\\species_13143\\species_13143',"areas",color='blue')
shp_info = map.readshapefile('C:\\Users\\Tharindu\\downloads\\Compressed\\LKA_adm1',"areas")
for index,row in sightings_dropped.iterrows():
    x,y = map(row['longitude'], row['latitude'])
    map.plot(x, y, 'green', markersize=7, alpha=0.8,marker='o',markeredgecolor='black')
map.drawmapscale(81.5, 5.8, 80.5, 6, 50) 

最佳答案

如果我理解正确的话...本质上,您所寻找的只是用任何颜色填充形状文件?这实际上列在 basemap 中 docs .

from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
import numpy as np

fig = plt.figure()
ax  = fig.add_subplot(111)

patches   = []

for info, shape in zip(map.comarques_info, map.comarques):
    patches.append( Polygon(np.array(shape), True) )

ax.add_collection(PatchCollection(patches, facecolor= 'm', edgecolor='k', linewidths=1., zorder=2))

我是列表理解的粉丝:

from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
import numpy as np

fig = plt.figure()
ax  = fig.add_subplot(111)

patches = [Polygon(np.array(shape), True) for info, shape in zip(m.states_info, m.states)]
ax.add_collection(PatchCollection(patches, facecolor= 'green', edgecolor='k', linewidths=1., zorder=2))

希望这对您有所帮助,并回答您的问题。

关于python - 使用 basemap 中的颜色填充 shp 文件中的多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47974693/

相关文章:

python - 为什么从`__iadd__`返回除“self”之外的任何东西?

R Leaflet GeoJSON着色

algorithm - 3D 平面多边形之间的交集

python - 图像中颜色的分类器

python - 使用Python将CSV数据加载到MySQL中,创建表并添加记录

python - 如何将等高线图叠加在 basemap 上

python - basemap 中的色标

python - matplotlib/ basemap 没有河流的世界地图?

Android Maps PolygonOptions Lat 和 Long 值(以公里为单位)

python - AssertListEqual 不改变类 __eq__ 方法