matplotlib之legend

  在《matplotlib极坐标系应用之雷达图》 中,我们提出了这个问题“图例中每种成员的颜色是怎样和极坐标相应的成员的颜色相对应的呢”,那么接下来我们说说legend的一般使用和设置。

调用legend()一般有三种方式:

方式1. 自动检测,直接调用legend();
  在plot()时就指定图例labels,再直接调用legend()就好了,或者在plot中指定plot elements,然后通过set_label函数指定图例labels

1 plt.plot([1, 2, 3], label=‘Inline label‘)
2 plt.legend()
3
4 line, = plt.plot([1, 2, 3])#此处的逗号非常重要,如果没有的话line是一个list对象;加上的话line是一个matplotlib.lines.Line2D对象,才能调用set_label()函数
5 line.set_label(‘Label via method‘)
6 plt.legend()

方式2. 显示指定labels,调用legend(labels);
  但是,这种方式会使plot elements 和 labels的对应关系不明显,所以并不建议使用这种方式。

  Note: This way of using is discouraged, because the relation between plot elements and labels is only implicit by their order and can easily be mixed up.

1 plt.plot([1, 2, 3])
2 plt.plot([1, 4, 9])
3 plt.legend([‘A simple line‘,‘2 simple line‘])

  

方式3. 显示指定plot elements 和 labels,调用legend(handles, labels)
  handles : sequence of Artist/lines/patches
    A list of Artists (lines, patches) to be added to the legend. Use this together with labels, if you need full control on what is shown in the legend and the automatic mechanism described above is not sufficient.
    The length of handles and labels should be the same in this case. If they are not, they are truncated to the smaller length.
  labels : sequence of strings, optional
    A list of labels to show next to the artists. Use this together with handles, if you need full control on what is shown in the legend and the automatic mechanism described above is not sufficient.

  

1 line1, = plt.plot([1,2,3])
2 print(type(line1))
3 line2, = plt.plot([1,4,9])
4 line3, = plt.plot([1,8,27])
5 handles = (line1, line2, line3)
6 labels = (‘label1‘, ‘label2‘, ‘label3‘)
7 plt.legend(handles, labels)

legend()的返回值:class:`matplotlib.legend.Legend` instance

其他参数说明:

loc : int or string or pair of floats, default: ‘upper right’

Location String Location Code
‘best’ 0
‘upper right’ 1
‘upper left’  2
‘lower left’  3
‘lower right’ 4
‘right’ 5
‘center left’ 6
‘center right’ 7
‘lower center’ 8
‘upper center’ 9
‘center’ 10

  ncol : integer(设置图例显示列数)
    The number of columns that the legend has. Default is 1.

  prop : None or matplotlib.font_manager.FontProperties or dict(设置图例字体)
    The font properties of the legend. If None (default), the current matplotlib.rcParams will be used.

references:

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html?highlight=legend#matplotlib.pyplot.legend

https://matplotlib.org/tutorials/intermediate/legend_guide.html#sphx-glr-tutorials-intermediate-legend-guide-py

原文地址:https://www.cnblogs.com/black-mamba/p/9082450.html

时间: 2024-08-29 00:57:01

matplotlib之legend的相关文章

matplotlib 进阶之Legend guide

目录 matplotlib.pyplot.legend 方法1自动检测 方法2为现有的Artist添加 方3显示添加图例 控制图例的输入 为一类Artist设置图例 Legend 的位置 loc, bbox_to_anchor 一个具体的例子 同一个Axes多个legend Legend Handlers 自定义图例处理程序 函数链接 import numpy as np import matplotlib.pyplot as plt matplotlib.pyplot.legend 在开始教程

matplotlib中plt.legend等的使用方法

plt.lengend() 用于给图像加图例. 图例是集中于地图一角或一侧的地图上各种符号和颜色所代表内容与指标的说明,有助于更好的认识地图. 语法参数如下: matplotlib.pyplot.legend(*args, **kwargs) keyword Description loc Location code string, or tuple (see below).图例所有figure位置 prop the font property字体参数 fontsize the font siz

python 2: 解决python中的plot函数的图例legend不能显示中文问题

 问题: 图像标题.横纵坐标轴的标签都能显示中文名字,但是图例就是不能显示中文,怎么解决呢?  解决: 1 plt.figure() 2 plt.title(u'训练性能', fontproperties=font) 3 plt.plot(history.epoch, history.history['loss'], label=u'训练误差') 4 plt.plot(history.epoch, history.history['val_loss'], label=u'验证误差') 5 plt

python可视化_matplotlib

对于Python数据可视化库,matplotlib 已经成为事实上的数据可视化方面最主要的库,此外还有很多其他库,例如vispy,bokeh, seaborn,pyga,folium 和 networkx,这些库有些是构建在 matplotlib 之上,还有些有其他一些功能. 目录 matplotlib 基本函数 中文乱码 plot:线性图 bar:柱状图 barh:水平柱状图 pie:饼图 scatter:散点图 hist:直方图 stackplot:面积图 subplot:子图布局 Grid

matplotlib中的legend()——用于显示图例

legend()的一个用法: 当我们有多个 axes时,我们如何把它们的图例放在一起呢?? 我们可以这么做: import matplotlib.pyplot as plt import numpy as np x = np.arange(1, 11) fig = plt.figure(1) ax1 = plt.subplot(2, 1, 1) ax2 = plt.subplot(2, 1, 2) l1, = ax1.plot(x, x*x, 'r') #这里关键哦 l2, = ax2.plot

matplotlib(3)-- legend(图例、说明、解释),annotate(标注),text(标注)

1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 x = np.linspace(-3, 3, 50) 5 y1 = 2 * x + 1 6 y2 = x ** 2 7 8 plt.figure() 9 l1, = plt.plot(x, y1, color = "red", linewidth = 5.0, linestyle = '--', label = 'down') #指定线的颜色, 宽度和类型 10 l2,

python matplotlib 中ax.legend()用法解释

ax.legend()作用:在图上标明一个图例,用于说明每条曲线的文字显示 import matplotlib.pyplot as plt import numpy as np x = np.arange(10) fig = plt.figure() ax = plt.subplot(111) for i in range(5): #ax.plot(x, i * x, label='y=%dx' %i) ax.plot(x, i * x, label='$y = %ix$' % i) ax.le

matplotlib

前导: 安装 numpy http://sourceforge.net/projects/numpy/files/ http://sourceforge.net/projects/numpy/files/NumPy/1.7.2/numpy-1.7.2-win32-superpack-python2.7.exe/download 安装 SciPy http://sourceforge.net/projects/scipy/files/ http://sourceforge.net/projects

【Python数据挖掘课程】六.Numpy、Pandas和Matplotlib包基础知识

前面几篇文章采用的案例的方法进行介绍的,这篇文章主要介绍Python常用的扩展包,同时结合数据挖掘相关知识介绍该包具体的用法,主要介绍Numpy.Pandas和Matplotlib三个包.目录:        一.Python常用扩展包        二.Numpy科学计算包        三.Pandas数据分析包        四.Matplotlib绘图包 前文推荐:       [Python数据挖掘课程]一.安装Python及爬虫入门介绍       [Python数据挖掘课程]二.K