Reputation: 143
I imported a shapefile with Wisconsin counties using geopandas
and each county's vote margin for a previous election. I am now trying to partition the state of Wisconsin into neighborhoods using a Voronoi tessellation of the vote share of each county. I want to use geoplot.voronoi
to do this, but geoplot
fails to plot my map and crashes when I call geoplot.voronoi
on my shapefile data.
The following code successfully generates a map of the vote share
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')
But, when I try to plot the same thing using geoplot
, I get a blank image (the apparent whitespace below is the image).
import geoplot
geoplot.polyplot(wi)
Furthermore, if I try to plot the Voronoi tessellation:
ax = geoplot.voronoi(
wi.sample(1000),
hue='mr', cmap='Reds', scheme='fisher_jenks',
clip=wi.geometry,
linewidth=0)
geoplot.polyplot(wi, ax=ax)
I get the following error:
/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'
Am I missing a step needed to plot geometries in geoplot
? I was following this example code, but they load their data from geopandas.datasets
, rather than supplying their own shapefiles. What am I doing wrong?
Thanks in advance for any and all help!
Upvotes: 4
Views: 3233
Reputation: 11
I just had a similar problem to this and found that my problem was that I had my LAT and LON values backwards. shapely.Point tuples
need to be (LON,LAT).
Another possible reason for a blank plot is that the plot is not blank at all, it is just a map of a very large region. I think this is unlikely for your plot but you can check this by passing a tuple with min and max LON and LAT values to the extent
variable in gplt.polyplot().
Upvotes: 1