简体   繁体   English

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

[英]Geopandas reduce legend size (and remove white space below map)

I would like to know how to change the legend automatically generated by Geopandas.我想知道如何更改 Geopandas 自动生成的图例。 Mostly I would like to reduce its size because it's quite big on the generated image.大多数情况下,我想减小它的大小,因为它在生成的图像上非常大。 The legend seems to take all the available space.图例似乎占用了所有可用空间。

Additional question, do you know how to remove the empty space below my map ?附加问题,您知道如何删除地图下方的空白区域吗? I've tried with我试过

pad_inches = 0, bbox_inches='tight' 

but I still have an empty space below the map.但我在地图下方还有一个空白区域。

在此处输入图片说明

Thanks for your help.谢谢你的帮助。

This works for me:这对我有用:

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

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

To show how to get proper size of a colorbar legend accompanying a map created by geopandas ' plot() method I use the built-in ' naturalearth_lowres ' dataset.为了展示如何获得由geopandas ' plot() 方法创建的地图附带的geopandas图例的适当大小,我使用内置的' naturalearth_lowres '数据集。

The working code is as follows.工作代码如下。

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

plot as usual, grab the axes 'ax' returned by the plot像往常一样绘图,获取绘图返回的轴“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))

map marginal/face deco地图边缘/面部装饰

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

colorbar will be created by ...颜色条将由...创建

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)))

at this stage, 'cbax' is just a blank axes, with un needed labels on x and y axes blank-out the array of the scalar mappable 'sm'在这个阶段,'cbax' 只是一个空白轴,在 x 和 y 轴上有不需要的标签,空白了标量可映射的 'sm' 数组

sm._A = []

draw colorbar into 'cbax'将颜色条绘制到“cbax”中

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

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

Read the comments in the code for useful info.阅读代码中的注释以获得有用的信息。

The resulting plot:结果图: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM