从GeoPandas图中重新放置图例 [英] Relocating legend from GeoPandas plot
问题描述
我正在使用GeoPandas绘图功能绘制带有图例的地图.当我绘图时,我的图例出现在该图的右上角.看起来是这样的:
I'm plotting a map with legends using the GeoPandas plotting function. When I plot, my legends appear in the upper right corner of the figure. Here is how it looks like:
我想将图例移到图形的下部.通常,对于正常的matplotlib图,我通常会这样做:
I wanted to move the legends to the lower part of the graph. I would normally would have done something like this for a normal matplotlib plot:
fig, ax = plt.subplots(1, figsize=(4.5,10))
lima_bank_num.plot(ax=ax, column='quant_cuts', cmap='Blues', alpha=1, legend=True)
ax.legend(loc='lower left')
但是,不考虑此修改.
However, this modification is not taken into account.
推荐答案
您可以使用ax.get_legend()
访问在ax
实例上定义的图例.然后,您可以使用方法set_bbox_to_anchor
更新图例的位置.从头开始创建图例时,这与loc
关键字的易用性不同,但是可以控制位置.因此,以您的示例为例,
You can access the legend defined on the ax
instance with ax.get_legend()
. You can then update the location of the legend using the method set_bbox_to_anchor
. This doesn't provide the same ease of use as the loc
keyword when creating a legend from scratch, but does give control over placement. So, for your example, something like:
leg = ax.get_legend()
leg.set_bbox_to_anchor((0., 0., 0.2, 0.2))
有点 set_bbox_to_anchor的文档,尽管我没有觉得它没有什么特别的帮助.
A bit of documentation of set_bbox_to_anchor, though I don't find it extraordinarily helpful.
这篇关于从GeoPandas图中重新放置图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!