2014-04-14 27 views
1

我不知道如何使用以下代碼。無論出於何種原因,GeoPandas * .plot()都不起作用,但我想對一些簡單的情節使用Pandas和GeoPandas。與Shapely Polygons的映射

我一直在嘗試從GeoPandas中取出Shapely對象並將它們繪製在底圖上。問題是多邊形不會繪圖。我從GeoPandas.geometry遍歷它們,將它們添加到軸集合,然後使用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() 
+0

怎麼了?詳細說明你的問題。 –

+0

什麼不與GeoPandas'plot()'方法一起使用?它尚未處理地圖投影,但它應該在數據的座標系中顯示一個圖。 – Kelsey

回答

2

檢查您的shape文件是在正確的投影系統。底圖目前設置爲墨卡託投影。之後,它爲我工作。