matplotlib 图例的标题

编程入门 行业动态 更新时间:2024-10-27 09:46:08
本文介绍了matplotlib 图例的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道为图例设置标题似乎相当多余,但是否可以使用 matplotlib?

I know it seems fairly redundant to have a title for a legend, but is it possible using matplotlib?

这是我拥有的代码片段:

Here's a snippet of the code I have:

import matplotlib.patches as mpatches import matplotlib.pyplot as plt one = mpatches.Patch(facecolor='#f3f300', label='label1', linewidth = 0.5, edgecolor = 'black') two = mpatches.Patch(facecolor='#ff9700', label = 'label2', linewidth = 0.5, edgecolor = 'black') three = mpatches.Patch(facecolor='#ff0000', label = 'label3', linewidth = 0.5, edgecolor = 'black') legend = plt.legend(handles=[one, two, three], loc = 4, fontsize = 'small', fancybox = True) frame = legend.get_frame() #sets up for color, edge, and transparency frame.set_facecolor('#b4aeae') #color of legend frame.set_edgecolor('black') #edge color of legend frame.set_alpha(1) #deals with transparency plt.show()

我想要label1上方的图例标题.供参考,以下是输出:

I would want the title of the legend above label1. For reference, this is the output:

推荐答案

在这一行添加title参数:

legend = plt.legend(handles=[one, two, three], title="title", loc=4, fontsize='small', fancybox=True)

另请参见官方 legend 构造函数的文档.

See also the official docs for the legend constructor.

更多推荐

matplotlib 图例的标题

本文发布于:2023-07-19 17:04:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1158567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图例   标题   matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!