plt.plot([0,1, 9], [3, 8, 2], label='asd') plt.legend(bbox_to_anchor=(0.2, 1.1)) plt.show()
1. plt.legend() 用于给图像加图例。 图例是集中于地图一角或一侧的地图上各种符号和颜色所代表内容与指标的说明,有助于更好的认识地图。 语法参数如下: matplotlib.pyplot.legend(*args, **kwargs) keywordDescription locLocation code string, or tuple (see below).图例所有figure位置 ...
# 如果为True,则图例标记位于图例标签的左侧 numpoints # the number of points in the legend for line. # 为线条图图例条目创建的标记点数 scatterpoints # the number of points in the legend for scatter plot. # 为散点图图例条目创建的标记点数 scatteryoffsets # a list of yoffsets for scatter sy...
这里需要做两件事,第一件事就是在我们通过plot绘制图像的时候需要加上label,表示当前画的这个图像到底是什么意思。这个label也就是我们看到左上角展示图例当中的文字。比如linear, quadratic之类的就是label。另外一个就是在我们调用show这个函数之前,需要调用一下legend这个方法,这个方法就是绘制图例用的。
plt.legend(loc='best',facecolor='blue') #设置图例背景颜色,若无边框,参数无效 1. 2. 3. 对于边框还可以采用面向对象方式: legend = plt.legend(["First", "Second"]) frame = legend.get_frame() frame.set_facecolor('blue') 1. 2.
在matplotlib 中 legend 用于设置图形中的图例,其常见用法如下: legend(loc # Location codestring, or tuple (see below). # 图例所有figure位置。 labels # 标签名称。 prop # the font property. # 字体参数 fontsize # the font size (used onlyifpropisnot specified). ...
plt.title("Simple Plot") plt.legend() plt.show() 它在子图上的用法基本上一模一样,我们来看个例子: 我们可以看到对于ax1这张子图来说,我们做的事情和plt是一样的,就是在调用plot的时候标上了label,然后在show之前调用了legend方法。 最后来介绍一下legend的参数,其实legend有很多参数,我们选择其中比较常用的...
l1,= ax1.plot(x, x*x,'r') #这里关键哦 l2,= ax2.plot(x, x*x,'b') # 注意 plt.legend([l1, l2], ['first','second'], loc ='upper right') #其中,loc表示位置的; plt.show() 在legend的参数中, loc参数设置图例的显示位置的: ...
这里需要做两件事,第一件事就是在我们通过plot绘制图像的时候需要加上label,表示当前画的这个图像到底是什么意思。这个label也就是我们看到左上角展示图例当中的文字。比如linear, quadratic之类的就是label。另外一个就是在我们调用show这个函数之前,需要调用一下legend这个方法,这个方法就是绘制图例用的。
plot([5, 6, 7]) ax.legend(['First line', 'Second line'])#或者直接在legend给出图例名称 ax.legend(loc='lower center', ncol=2)#通过loc设置图例位置 图例位置legend loc参数 'upper left', 'upper right', 'lower left', 'lower right' 字符串将图例放在坐标轴相应的角上。 'upper center', ...