使用底图中的颜色在shp文件中填充多边形

时间:2017-12-26 06:25:14

标签: python matplotlib-basemap polygons

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

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) 

1 个答案:

答案 0 :(得分:2)

如果我正确理解这一点......从本质上讲,你所寻找的只是用任何颜色填充一个shapefile?这实际上列在底图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))

希望这有帮助,我回答你的问题。