【问题标题】:How to remove border and resize label title of a geopandas legend如何删除 geopandas 图例的边框和调整标签标题的大小
【发布时间】:2021-01-27 16:34:38
【问题描述】:

所以我有以下问题,我想删除 geopandas 中彩色世界地图图例的黑色边框并调整标签标题的大小,但我不知道该怎么做。

这是我的代码:

f, ax = plt.subplots(1, figsize=(50, 10))
fig.subplots_adjust(hspace=0.0, wspace=0.0)

ax=merge.plot(column="counts", legend=True,  missing_kwds= dict(color = "lightgrey"), 
            legend_kwds= {'label': "Medal Count" , 'orientation': "horizontal", 'shrink': 0.2}, ax=ax, cmap='YlOrRd')

ax.set_axis_off()

fig = ax.figure
cb_ax = fig.axes[1] 
cb_ax.tick_params(labelsize=15, left=False, labelleft=False, bottom=False)




NaN = mpatches.Patch(color='lightgrey', label='No medals for Gymnastics')
plt.legend(handles=[NaN])

这是我得到的:

【问题讨论】:

  • @PaulH 试过了,对 geopandas 颜色图例不起作用,或者我可能做错了。如果我使用它,我会遇到这个错误:没有找到带有标签的句柄放在图例中。

标签: python legend geopandas


【解决方案1】:

您必须抓住图例的手柄才能使用。下面的代码sn -p显示图例的句柄leg2被抓取并使用。

leg2 = plt.legend(handles=[NaN], 
                  fontsize="large",   # set font size
                  mode="expand",      # allow `bbox_to_anchor` to set position/size
                  bbox_to_anchor=(0.15, 0.80, 0.75, 0.2))
# fontsize: 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'
# bbox_to_anchor: (x, y, width, height)

# make use of the legend's handle, leg2
leg2.get_frame().set_edgecolor('none')  # remove edge line

【讨论】:

    猜你喜欢
    • 2014-04-17
    • 2022-08-19
    • 2021-10-02
    • 2013-07-11
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多