2017-06-18 38 views
1

我知道它有一个传说的标题似乎相当多余,但它可能使用matplotlib?Python - 传说matplotlib的标题

这里是我的代码的一个片段:

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传奇的称号。作为参考,这是输出: Showing legend

+0

实施例1有一个标题https://matplotlib.org/examples/pylab_examples/legend_demo3.html –

+1

出于兴趣,你能说出是什么原因让你提出这个问题,花了很多精力写下它,甚至放入了一张图片,而不是像[matplotlib legend title](http://www.google。 de/search?q = title + legend + matplotlib)还是查看文档? – ImportanceOfBeingErnest

回答

2

只需添加标题参数给此线

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