用Shapely多边形映射

2024-05-29 10:40:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法让下面的代码正常工作。不管出于什么原因,GeoPandas*.plot()不起作用,但是我想在一些简单的绘图中同时使用Pandas和GeoPandas。在

我试着从地图上取下物体的形状。问题是多边形无法绘制。我从地质学几何学,将它们添加到axes集合中,然后使用plot()-无效。基本地图似乎工作良好,代码没有给出任何错误,但多边形-县-没有出现。。。在

谢谢你的帮助!在

import geopandas as gpd
from descartes import PolygonPatch
import matplotlib as mpl
import mpl_toolkits.basemap as base
import matplotlib.pyplot as plt

counties_file = r'C:\Users\...\UScounties\UScounties.shp'
counties = gpd.read_file(counties_file)

#new plot
fig = plt.figure(figsize=(5,5),dpi=300)
#ax = fig.add_subplot(111)
ax = ax = plt.gca()

minx, miny, maxx, maxy = counties.total_bounds

#map
m = base.Basemap(llcrnrlon=minx, llcrnrlat=miny,
             urcrnrlon=maxx, urcrnrlat=maxy,
             resolution='h', area_thresh=100000,
             projection='merc')

patches = []

#add polygons
for poly in counties.geometry:
    #deal with single polygons and multipolygons
    if poly.geom_type == 'Polygon':
        p = PolygonPatch(poly, facecolor='blue', alpha=1)
        #plt.gca().add_patch(p)
        #ax.add_patch(p)
        patches.append(p)

    elif poly.geom_type == 'MultiPolygon':
        for single in poly:
            q = PolygonPatch(single,facecolor='red', alpha=1)
            #ax.add_patch(p)
            patches.append(q)

m.drawcoastlines(linewidth=.1)
m.fillcontinents()
m.drawcountries(linewidth=.25,linestyle='solid')
m.drawstates(linewidth=.25,linestyle='dotted')
m.drawmapboundary(fill_color='white')

ax.add_collection(mpl.collections.PatchCollection(patches, match_original=True))
ax.plot()

plt.show()

Tags: importaddplotaspltaxfilempl

热门问题