本文介绍了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 图例的标题
发布评论