Geopandas 减少图例大小(并删除地图下方的空白区域)

问题描述 投票:0回答:2

我想知道如何更改Geopandas自动生成的图例。大多数情况下,我想减小它的大小,因为它在生成的图像上相当大。图例似乎占据了所有可用空间。

附加问题,您知道如何删除地图下方的空白区域吗?我尝试过

pad_inches = 0, bbox_inches='tight' 

但我在地图下方仍然有一个空白区域。

感谢您的帮助。

legend geopandas legend-properties
2个回答
27
投票

这对我有用:

some_geodataframe.plot(..., legend=True, legend_kwds={'shrink': 0.3})

在较新的版本中,这将是:

some_geodataframe.plot(..., legend=True, legend_kwargs={'shrink': 0.3})

此处的其他选项:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.colorbar.html


3
投票

为了展示如何获得由

geopandas
'plot() 方法创建的地图所附的颜色条图例的正确大小,我使用内置的 'naturalearth_lowres' 数据集。

工作代码如下。

import matplotlib.pyplot as plt
import geopandas as gpd

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world[(world.name != "Antarctica") & (world.name != "Fr. S. Antarctic Lands")]  # exclude 2 no-man lands

照常绘图,抓住绘图返回的轴“ax”

colormap = "copper_r"   # add _r to reverse the colormap
ax = world.plot(column='pop_est', cmap=colormap, \
                figsize=[12,9], \
                vmin=min(world.pop_est), vmax=max(world.pop_est))

地图边缘/面部装饰

ax.set_title('World Population')
ax.grid() 

颜色条将由...创建

fig = ax.get_figure()
# add colorbar axes to the figure
# here, need trial-and-error to get [l,b,w,h] right
# l:left, b:bottom, w:width, h:height; in normalized unit (0-1)
cbax = fig.add_axes([0.95, 0.3, 0.03, 0.39])   
cbax.set_title('Population')

sm = plt.cm.ScalarMappable(cmap=colormap, \
                norm=plt.Normalize(vmin=min(world.pop_est), vmax=max(world.pop_est)))

在此阶段,“cbax”只是一个空白轴,x 轴和 y 轴上不需要的标签会清空可映射标量“sm”的数组

sm._A = []

将颜色条绘制到'cbax'中

fig.colorbar(sm, cax=cbax, format="%d")

# dont use: plt.tight_layout()
plt.show()

阅读代码中的注释以获取有用的信息。

结果图:

© www.soinside.com 2019 - 2024. All rights reserved.