【问题标题】:Relocating legend from GeoPandas plot从 GeoPandas 图中重新定位图例
【发布时间】:2023-04-05 23:10:02
【问题描述】:

我正在使用 GeoPandas 绘图功能绘制带有图例的地图。当我绘图时,我的图例出现在图形的右上角。这是它的样子:

我想将图例移动到图表的下部。我通常会为正常的 matplotlib 图做这样的事情:

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

但是,不考虑此修改。

【问题讨论】:

    标签: matplotlib geopandas


    【解决方案1】:

    这可以使用legend_kwds 参数来完成:

    df.plot(column='values', legend=True, legend_kwds={'loc': 'lower right'});
    

    【讨论】:

      【解决方案2】:

      您可以使用ax.get_legend() 访问在ax 实例上定义的图例。然后,您可以使用方法set_bbox_to_anchor 更新图例的位置。从头开始创建图例时,这不提供与 loc 关键字相同的易用性,但可以控制放置。因此,对于您的示例,类似:

      leg = ax.get_legend()
      leg.set_bbox_to_anchor((0., 0., 0.2, 0.2))
      

      一点documentation of set_bbox_to_anchor,虽然我不觉得它特别有用。

      【讨论】:

        【解决方案3】:

        如果您有一个水平图例,并且您想简单地缩小图例和绘图之间的差距,我建议使用 colorbar 方法,详细信息在 https://gis.stackexchange.com/a/330175/32531 以及传递 pad legend_kwd 参数:

        legend_kwds={"orientation": "horizontal", "pad": 0.01}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-07
          • 2020-05-06
          • 2018-06-23
          • 2019-09-05
          • 1970-01-01
          相关资源
          最近更新 更多