马春杰杰 Exit Reader Mode

matplotlib设置图例中marker大小 | plt设置图例中图标的大小

一般matplotlib中图例marker的大小是跟图中的大小一致的,但是有时候数据点多了,图中的应该设置很小,但是图例中的我们希望设置大点,比如:

可以看到,图例中的marker非常小,这是非常不好看的,修改方法如下:

from matplotlib.collections import PathCollection
from matplotlib.legend_handler import HandlerLine2D
def updateline(handle, orig):
    handle.update_from(orig)
    handle.set_markersize(30)

首先增加以上定义,然后在ax.legend中加入handler_map={plt.Line2D: HandlerLine2D(update_func=updateline)}即可,比如原来的是:

    ax.legend(framealpha=1, 
              frameon=True, fontsize=60)

加上之后变成:

    ax.legend(framealpha=1, handler_map={plt.Line2D: HandlerLine2D(update_func=updateline)},
              frameon=True, fontsize=60)

效果:

 

本文最后更新于2021年7月9日,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!