python - 使用匀称多边形进行映射

标签 python plot matplotlib-basemap geopandas

我无法让以下代码正常工作。无论出于何种原因,GeoPandas *.plot() 不起作用,但我想同时使用 Pandas 和 GeoPandas 来绘制一些简单的绘图。

我一直在尝试从 GeoPandas 中获取 Shapely 对象并将它们绘制在 basemap 上。问题是多边形不会绘制。我从 GeoPandas.geometry 迭代它们,将它们添加到轴集合中,然后使用 plot() - 无济于事。 basemap 似乎工作正常,代码没有给出任何错误,但多边形 - 县 - 没有出现......

感谢您的帮助!

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

最佳答案

检查您的 shapefile 是否位于正确的投影系统中。 basemap 当前设置为墨卡托投影。之后它对我有用。

关于python - 使用匀称多边形进行映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23071955/

相关文章:

使用 matplotlib/basemap 进行 Python 插值

python - 向 matplotlib Figure 和 Axes 实例添加自定义属性 : ill-advised?

python - 通过 QValidator 验证可编辑的 QCombobox 输入是否是目录

python - 将列转换为 Pandas 数据框中的表头

python - Matplotlib 无法识别属性 set_xdata。

r - 在 (r)gedit 中显示多个 R 图形窗口

R x 轴日期标签只有一个值

python - __slot__ 描述符如何在 python 中工作?

python - 在Python中更改像素颜色: How to do it faster?

Matlab:用3个向量制作等高线图