概述
matplotlib主要通过代理对象实现自定义图例。根据官方文档https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html,通过创建自定义类构造legend_artist方法即可实现emoj图例。其步骤大致为:
- 获取图例可视对象容器
handlebox的位置信息。 - 构造图例可视对象。
- 将可视对象添加到图例可视对象容器
handlebox中。
legend_artist方法的具体说明及源码如下。
https://matplotlib.org/stable/api/legend_handler_api.html#matplotlib.legend_handler.HandlerBase.legend_artist
def legend_artist(self, legend, orig_handle,
fontsize, handlebox):
"""
Return the artist that this HandlerBase generates for the given
original artist/handle.
Parameters
----------
legend : `~matplotlib.legend.Legend`
The legend for which these legend artists are being created.
orig_handle : :class:`matplotlib.artist.Artist` or similar
The object for which these legend artists are being created.
fontsize : int
The fontsize in pixels. The artists being created should
be scaled according to the given fontsize.
handlebox : `matplotlib.offsetbox.OffsetBox`
The box which has been created to hold this legend entry's
artists. Artists created in the `legend_artist` method must
be added to this handlebox inside this method.
"""
xdescent, ydescent, width, height = self.adjust_drawing_area(
legend, orig_handle,
handlebox.xdescent, handlebox.y

本文介绍了如何使用Matplotlib创建自定义图例,通过创建代理对象和EmojHandler类,展示了如何为特定对象添加个性化的emoji图例。步骤包括调整绘图区域、构造并添加艺术家到图例容器。实例演示了如何为不同对象映射不同颜色的emoji图例。
最低0.47元/天 解锁文章
1645

被折叠的 条评论
为什么被折叠?



