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

Arn*_*ibu 5 legend legend-properties geopandas

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

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

pad_inches = 0, bbox_inches='tight' 
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

谢谢你的帮助。

Chr*_*ers 9

这对我有用:

some_geodataframe.plot(..., legend=True, legend_kwds={'shrink': 0.3})
Run Code Online (Sandbox Code Playgroud)

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

  • “简单总比复杂好”-谢谢! (2认同)

swa*_*hai 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
Run Code Online (Sandbox Code Playgroud)

像往常一样绘图,抓住绘图返回的轴“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))
Run Code Online (Sandbox Code Playgroud)

地图边缘/面装饰

ax.set_title('World Population')
ax.grid() 
Run Code Online (Sandbox Code Playgroud)

颜色条将由...创建

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)))
Run Code Online (Sandbox Code Playgroud)

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

sm._A = []
Run Code Online (Sandbox Code Playgroud)

将颜色条绘制到“cbax”中

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

# dont use: plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

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

结果图: 在此输入图像描述