简体   繁体   English

使用匀称多边形进行映射

[英]Mapping with Shapely Polygons

I am at a loss to get the following code to work. 我无法获得以下代码。 For whatever reason, the GeoPandas *.plot() doesn't work, but I want to use both Pandas and GeoPandas for some simple plots. 无论出于何种原因,GeoPandas * .plot()都不起作用,但我想在一些简单的图中使用Pandas和GeoPandas。

I have been trying to take the Shapely objects from GeoPandas and plot them on a Basemap. 我一直在尝试从GeoPandas获取Shapely对象并将它们绘制在Basemap上。 The problem is that the polygons won't plot. 问题是多边形不会绘制。 I iterate through them from the GeoPandas.geometry, add them to the axes collection, and then use plot() - to no avail. 我从GeoPandas.geometry迭代它们,将它们添加到轴集合,然后使用plot() - 无济于事。 Basemap seems to work fine, the code doesn't give any errors, but the polygons - counties - don't appear... 底图似乎工作正常,代码不会给出任何错误,但多边形 - 县 - 不会出现......

Thank you for the help! 感谢您的帮助!

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()

Check if your shapefile is in the right projection system. 检查shapefile是否在正确的投影系统中。 Basemap is currently set to Mercator Projection. 底图目前设置为墨卡托投影。 After that it worked for me. 之后它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM