gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:20:41 25 4
gpt4 key购买 nike

我创建了一个点图,并使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com