我想知道如何更改Geopandas自动生成的图例。通常,我想减小它的大小,因为它在生成的图像上很大。图例似乎占用了所有可用空间。
其他问题,您知道如何删除地图下方的空白区域吗?我尝试使用pad_inches = 0,bbox_inches ='tight',但在地图下方仍然有空白区域。
感谢您的帮助。
答案 0 :(得分:2)
这对我有用:
some_geodataframe.plot(..., legend=True, legend_kwds={'shrink': 0.3})
此处的其他选项:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.colorbar.html
答案 1 :(得分:0)
要显示如何获得由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
# plot as usual, grab the axes 'ax' returned by the plot
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'
sm._A = []
# draw colorbar into 'cbax'
fig.colorbar(sm, cax=cbax, format="%d")
# dont use: plt.tight_layout()
plt.show()
阅读代码中的注释以获取有用的信息。