使用底图中的颜色填充 shp 文件中的多边形
Filling the polygons in shp file by using colors in basemap
我创建了一个点地图,并使用 python 底图在其上叠加了县边界。我有另一个基于 IUCN 数据 (species_13143) 的物种分布图的 shapefile。我已将该形状文件覆盖到之前的地图上。它运作良好。但不幸的是,它没有填满与其关联的多边形。我想按照这张地图使用颜色来填充这些多边形(我不介意颜色;单一颜色也可以)
。
我在 Whosebug 中发现了类似的问题。但是 none 对我来说是有效的。代码和相关图片附在此处..
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)
如果我没有理解错的话...从本质上讲,您要找的只是用任何颜色填充一个 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))
希望这对您有所帮助,我会回答您的问题。
我创建了一个点地图,并使用 python 底图在其上叠加了县边界。我有另一个基于 IUCN 数据 (species_13143) 的物种分布图的 shapefile。我已将该形状文件覆盖到之前的地图上。它运作良好。但不幸的是,它没有填满与其关联的多边形。我想按照这张地图使用颜色来填充这些多边形(我不介意颜色;单一颜色也可以)
我在 Whosebug 中发现了类似的问题。但是 none 对我来说是有效的。代码和相关图片附在此处..
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)
如果我没有理解错的话...从本质上讲,您要找的只是用任何颜色填充一个 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))
希望这对您有所帮助,我会回答您的问题。