4

我正在寻找一种方法来格式化/舍入由.plot()GeoPandas 中的函数生成的那些地图中的数字图例标签。例如:

gdf.plot(column='pop2010', scheme='QUANTILES', k=4)

这给了我一个带有许多小数位的图例:

在此处输入图像描述

我希望图例标签是整数。

4

2 回答 2

7

由于我最近遇到了同样的问题,并且在 Stack Overflow 或其他网站上似乎没有现成的解决方案,我想我会发布我采用的方法以防万一它有用。

首先,使用geopandas世界地图的基本情节:

# load world data set    
world_orig = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world = world_orig[(world_orig['pop_est'] > 0) & (world_orig['name'] != "Antarctica")].copy()
world['gdp_per_cap'] = world['gdp_md_est'] / world['pop_est']

# basic plot
fig = world.plot(column='pop_est', figsize=(12,8), scheme='fisher_jenks', 
                 cmap='YlGnBu', legend=True)
leg = fig.get_legend()
leg._loc = 3
plt.show()

世界地图 v1

我使用的方法依赖于对象的get_texts()方法matplotlib.legend.Legend,然后遍历 中的项目leg.get_texts(),将文本元素拆分为下限和上限,然后创建一个应用格式的新字符串并使用该set_text()方法设置它。

# formatted legend
fig = world.plot(column='pop_est', figsize=(12,8), scheme='fisher_jenks', 
                 cmap='YlGnBu', legend=True)
leg = fig.get_legend()
leg._loc = 3

for lbl in leg.get_texts():
    label_text = lbl.get_text()
    lower = label_text.split()[0]
    upper = label_text.split()[2]
    new_text = f'{float(lower):,.0f} - {float(upper):,.0f}'
    lbl.set_text(new_text)

plt.show()

这在很大程度上是一种“反复试验”的方法,所以如果有更好的方法,我不会感到惊讶。不过,也许这会有所帮助。

世界地图 v2

于 2019-06-14T03:43:38.713 回答
5

方法一:

GeoPandas 使用 PySal 的mapclassify。这是分位数图的示例(k = 5)。

import matplotlib.pyplot as plt
import numpy as np
import mapclassify   # 2.3.0
import geopandas as gpd   # 0.8.1

# load dataset
path = gpd.datasets.get_path('naturalearth_lowres')
gdf = gpd.read_file(path)
# generate a random column
np.random.seed(0)
gdf['random_col'] = np.random.normal(100, 10, len(gdf))

# plot quantiles map
fig, ax = plt.subplots(figsize=(10, 10))
gdf.plot(column='random_col', scheme='quantiles', k=5, cmap='Blues',
         legend=True, legend_kwds=dict(loc=6), ax=ax)

这给了我们:

在此处输入图像描述

假设我们要对图例中的数字进行四舍五入。我们可以通过.Quantiles()中的函数得到分类mapclassify

mapclassify.Quantiles(gdf.random_col, k=5)

该函数返回一个对象mapclassify.classifiers.Quantiles

Quantiles               

    Interval       Count
------------------------
[ 74.47,  91.51] |    36
( 91.51,  97.93] |    35
( 97.93, 103.83] |    35
(103.83, 109.50] |    35
(109.50, 123.83] |    36

该对象有一个属性bins,它返回一个包含所有类的上限的数组。

array([ 91.51435701,  97.92957441, 103.83406507, 109.49954895,
       123.83144775])

因此,我们可以使用此函数来获取类的所有边界,因为较低类的上限等于较高类的下限。唯一缺少的是最低类的下限,它等于您尝试在 DataFrame 中分类的列的最小值。

这是一个将所有数字四舍五入为整数的示例:

# get all upper bounds
upper_bounds = mapclassify.Quantiles(gdf.random_col, k=5).bins

# get and format all bounds
bounds = []
for index, upper_bound in enumerate(upper_bounds):
    if index == 0:
        lower_bound = gdf.random_col.min()
    else:
        lower_bound = upper_bounds[index-1]

    # format the numerical legend here
    bound = f'{lower_bound:.0f} - {upper_bound:.0f}'
    bounds.append(bound)

# get all the legend labels
legend_labels = ax.get_legend().get_texts()

# replace the legend labels
for bound, legend_label in zip(bounds, legend_labels):
    legend_label.set_text(bound)

我们最终会得到: 在此处输入图像描述


方法二:

除了 GeoPandas 的.plot()方法,您还可以考虑geoplot.choropleth()提供的功能,您可以在其中轻松使用不同类型的方案和类数,同时传递arg 来修改图例标签。例如,legend_labels

import geopandas as gpd
import geoplot as gplt

path = gpd.datasets.get_path('naturalearth_lowres')
gdf = gpd.read_file(path)

legend_labels = ['< 2.4', '2.4 - 6', '6 - 15', '15 - 38', '38 - 140 M']
gplt.choropleth(gdf, hue='pop_est', cmap='Blues', scheme='quantiles',
                legend=True, legend_labels=legend_labels)

这给了你

在此处输入图像描述

于 2019-06-26T14:07:19.213 回答