我的shapefile在某些列(例如GDP)上缺少一些值(用nan
表示)。当在不处理那些缺失值的情况下进行绘制时,图例显示如下:
enter image description here
这不是我想要的。
因此,我将丢失的值替换为字符串“ missing”,然后重做绘图。毫不奇怪,我收到错误消息,说TypeError: '<' not supported between instances of 'str' and 'float'
。
我的问题是:1. Geopandas如何对待缺失的价值观?是否将丢失的值存储在字符串或其他类型的数据中? 2.如何保留这些缺失值并重做带有图例标签的绘图以显示缺失?
答案 0 :(得分:1)
import numpy as np
import matplotlib.pyplot as plt
import geopandas as gpd
import pysal.viz.mapclassify as mc
from matplotlib.colors import rgb2hex
plt.style.use('seaborn')
gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# generate random data
gdf['random'] = np.random.normal(100, 10, len(gdf))
# assign missing values
gdf.loc[np.random.choice(gdf.index, 40), 'random'] = np.nan
这里的基本思想是根据要用于数字数据的分类方法(例如分位数,百分位数等)创建类别/字符串列。之后,我们绘制该字符串列,以便我们可以传递自定义的颜色图(用灰色表示缺失值)。
# categorize the numerical column
k = 5
quantiles = mc.Quantiles(gdf.random.dropna(), k=k)
gdf['random_cat'] = quantiles.find_bin(gdf.random).astype('str')
gdf.loc[gdf.random.isnull(), 'random_cat'] = 'No Data'
# add grey to a colormap to represent missing value
cmap = plt.cm.get_cmap('Blues', k)
cmap_list = [rgb2hex(cmap(i)) for i in range(cmap.N)]
cmap_list.append('grey')
cmap_with_grey = colors.ListedColormap(cmap_list)
# plot map
fig, ax = plt.subplots(figsize=(12, 10))
gdf.plot(column='random_cat', edgecolor='k', cmap=cmap_with_grey,
legend=True, legend_kwds=dict(loc='center left'),
ax=ax)
# get all upper bounds in the quantiles category
upper_bounds = quantiles.bins
# get and format all bounds
bounds = []
for index, upper_bound in enumerate(upper_bounds):
if index == 0:
lower_bound = gdf.random.min()
else:
lower_bound = upper_bounds[index-1]
bound = f'{lower_bound:.2f} - {upper_bound:.2f}'
bounds.append(bound)
# get all the legend labels
legend_labels = ax.get_legend().get_texts()
# replace the numerical legend labels
for bound, legend_label in zip(bounds, legend_labels):
legend_label.set_text(bound)
您可能想看看以下帖子:
format/round numerical legend label in GeoPandas
Extract matplotlib colormap in hex-format
答案 1 :(得分:1)
更新:geopandas
中的新功能解决了您的问题:您可以将缺少的值保留为NaN
并使用:
ax = gdf.plot( <other arguments>,
missing_kwds = dict(color = "lightgrey",) )
使所有丢失的数据区域变为浅灰色。
请参见https://geopandas.readthedocs.io/en/latest/mapping.html
(实际上,文档可能会说该参数为missing_kwdsdict
,但是上面的内容对我有用)
答案 2 :(得分:0)
GeoPandas目前不支持绘制缺失值。计划在0.7版本中发布。可能的解决方案是仅绘制那些没有缺失值的行,然后仅绘制缺少值。由于您没有给我们提供任何代码,因此以下是https://nbviewer.jupyter.org/gist/jorisvandenbossche/bb1cc71f94aa3e8f2832f18dd12f6174
中的示例import geopandas
gdf = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
# Introduce some missing values:
gdf.loc[np.random.choice(gdf.index, 20), 'pop_est'] = np.nan
ax = gdf[gdf.pop_est.notna()].plot(column='pop_est', figsize=(15, 10), legend=True)
gdf[gdf.pop_est.isna()].plot(color='lightgrey', hatch='///', ax=ax)