gpt4 book ai didi

python - Geoplot 不会绘制 geopandas 对象

转载 作者:行者123 更新时间:2023-12-01 08:36:20 24 4
gpt4 key购买 nike

我使用 geopandas 导入了威斯康星州各县的 shapefile 以及每个县上次选举的选票差额。我现在尝试使用每个县的投票份额的 Voronoi 曲面分割将威斯康星州划分为多个社区。我想使用 geoplot.voronoi 来执行此操作,但在 shapefile 上调用 geoplot.voronoi 时,geoplot 无法绘制 map 并崩溃数据。

以下代码成功生成了投票份额的 map

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 绘制相同的内容时,我得到一个空白图像(下面的明显空白图像)。

import geoplot
geoplot.polyplot(wi)

<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。我做错了什么?

预先感谢您提供的所有帮助!

最佳答案

我刚刚遇到了与此类似的问题,发现我的问题是我的 LAT 和 LON 值向后。 shapely.Point 元组 需要是 (LON,LAT)。

空白图的另一个可能原因是该图根本不是空白的,它只是一个非常大区域的 map 。我认为这对于您的绘图来说不太可能,但您可以通过将具有最小和最大 LON 和 LAT 值的元组传递给 gplt.polyplot() 中的 extent 变量来检查这一点。

关于python - Geoplot 不会绘制 geopandas 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53707430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com