Geoplot不会绘制geopandas obj

2024-09-28 19:29:07 发布

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

我使用geopandas和每个县在上一次选举中的票数差额,导入了一个与威斯康星州各县的shapefile。我现在正试图用沃罗诺(Voronoi)细分每个县的投票份额,把威斯康星州划分为多个社区。我想使用geoplot.voronoi来完成这项工作,但是当我对shapefile数据调用geoplot.voronoi时,geoplot无法绘制地图并崩溃。在

下面的代码成功地生成了投票份额的映射

import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gp

wi = gp.GeoDataFrame.from_file('./data/shape/Wards_111312.shp')
elec_dat = pd.read_csv('./data/wi_2014_heda.csv')

wi['vrat'] = elec_dat['g2014_SOS_rv']
wi['vdat'] = elec_dat['g2014_SOS_dv']
wi['mr'] = (wi['vdat'] - wi['vrat']) / ((wi['vdat'] + wi['vrat']))
wi['mr'] = wi['mr'].fillna(0)

wi.plot(column = 'mr', cmap='OrRd')

<code>geopandas</code> plotted margin map

但是,当我试图用geoplot绘制相同的东西时,我得到了一个空白图像(下面的明显空白是图像)。在

^{pr2}$

<code>geoplot</code> plotted map (blank)

此外,如果我尝试绘制Voronoi细分图:

ax = geoplot.voronoi(
    wi.sample(1000),
    hue='mr', cmap='Reds', scheme='fisher_jenks',
    clip=wi.geometry,
    linewidth=0)
geoplot.polyplot(wi, ax=ax)

我得到以下错误:

/home/rtse/anaconda3/envs/gerry/lib/python3.6/site-packages/pysal/__init__.py:65: VisibleDeprecationWarning: PySAL's API will be changed on 2018-12-31. The last release made with this API is version 1.14.4. A preview of the next API version is provided in the `pysal` 2.0 prelease candidate. The API changes and a guide on how to change imports is provided at https://pysal.org/about
  ), VisibleDeprecationWarning)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-5ed5e333efcd> in <module>
      7     hue='mr', cmap='Reds', scheme='fisher_jenks',
      8     clip=wi.geometry,
----> 9     linewidth=0)
     10 geoplot.polyplot(wi, ax=ax)
     11 

~/anaconda3/envs/gerry/lib/python3.6/site-packages/geoplot/geoplot.py in voronoi(df, projection, edgecolor, clip, hue, scheme, k, cmap, categorical, vmin, vmax, legend, legend_kwargs, legend_labels, extent, figsize, ax, **kwargs)
   2126 
   2127     # Finally we draw the features.
-> 2128     geoms = _build_voronoi_polygons(df)
   2129     if projection:
   2130         for color, geom in zip(colors, geoms):

~/anaconda3/envs/gerry/lib/python3.6/site-packages/geoplot/geoplot.py in _build_voronoi_polygons(df)
   2628     """
   2629     from scipy.spatial import Voronoi
-> 2630     geom = np.array(df.geometry.map(lambda p: [p.x, p.y]).tolist())
   2631     vor = Voronoi(geom)
   2632 

~/anaconda3/envs/gerry/lib/python3.6/site-packages/pandas/core/series.py in map(self, arg, na_action)
   2996         """
   2997         new_values = super(Series, self)._map_values(
-> 2998             arg, na_action=na_action)
   2999         return self._constructor(new_values,
   3000                                  index=self.index).__finalize__(self)

~/anaconda3/envs/gerry/lib/python3.6/site-packages/pandas/core/base.py in _map_values(self, mapper, na_action)
   1002 
   1003         # mapper is a function
-> 1004         new_values = map_f(values, mapper)
   1005 
   1006         return new_values

pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()

~/anaconda3/envs/gerry/lib/python3.6/site-packages/geoplot/geoplot.py in <lambda>(p)
   2628     """
   2629     from scipy.spatial import Voronoi
-> 2630     geom = np.array(df.geometry.map(lambda p: [p.x, p.y]).tolist())
   2631     vor = Voronoi(geom)
   2632 

AttributeError: 'MultiPolygon' object has no attribute 'x'

我是否遗漏了在geoplot中绘制几何图形所需的步骤?我跟踪this example code,但是他们从geopandas.datasets加载数据,而不是提供自己的shapefile。我做错什么了?在

提前感谢您的帮助!在


Tags: inmaplibpackagessiteaxvoronoivalues
1条回答
网友
1楼 · 发布于 2024-09-28 19:29:07

我只是有一个类似的问题,我发现我的问题是我有我的纬度和经度值倒退。shapely.Point tuples必须是(LON,LAT)。在

空白图的另一个可能的原因是,它根本不是空白的,它只是一个非常大区域的地图。我认为这对于您的绘图来说不太可能,但是您可以通过向中的extent变量传递一个包含min和max LON和LAT值的元组来检查这一点gplt.polyplot公司(). 在

相关问题 更多 >