matplotlib之legend
在《matplotlib极坐标系应用之雷达图》 中,我们提出了这个问题“图例中每种成员的颜色是怎样和极坐标相应的成员的颜色相对应的呢”,那么接下来我们说说legend的一般使用和设置。
调用legend()一般有三种方式:
方式1. 自动检测,直接调用legend();
在plot()时就指定图例labels,再直接调用legend()就好了,或者在plot中指定plot elements,然后通过set_label函数指定图例labels
plt.plot([1, 2, 3], label='Inline label')
plt.legend() line, = plt.plot([1, 2, 3])#此处的逗号非常重要,如果没有的话line是一个list对象;加上的话line是一个matplotlib.lines.Line2D对象,才能调用set_label()函数
line.set_label('Label via method')
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.
plt.plot([1, 2, 3])
plt.plot([1, 4, 9])
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.
line1, = plt.plot([1,2,3])
print(type(line1))
line2, = plt.plot([1,4,9])
line3, = plt.plot([1,8,27])
handles = (line1, line2, line3)
labels = ('label1', 'label2', 'label3')
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
matplotlib之legend的更多相关文章
- matplotlib笔记——legend用法
rates = [0.01, 0.001, 0.0001] models = {} costs = np.array([[0.7, 0.9, 0.4, 0.6, 0.4, 0.3, 0.2, 0.1] ...
- 【Python学习笔记】调整matplotlib的图例legend的位置
有时默认的图例位置不符合我们的需要,那么我们可以使用下面的代码对legend位置进行调整. plt.legend(loc='String or Number', bbox_to_anchor=(num ...
- matplotlib.legend()函数用法
用的较多,作为记录 legend语法参数如下: matplotlib.pyplot.legend(*args, **kwargs) 几个暂时主要用的参数: (1)设置图例位置 使用loc参数 plt. ...
- matplotlib 进阶之Legend guide
目录 matplotlib.pyplot.legend 方法1自动检测 方法2为现有的Artist添加 方3显示添加图例 控制图例的输入 为一类Artist设置图例 Legend 的位置 loc, b ...
- matplotlib 入门之Sample plots in Matplotlib
文章目录 Line Plot One figure, a set of subplots Image 展示图片 展示二元正态分布 A sample image Interpolating images ...
- 基于matplotlib的数据可视化 - 笔记
1 基本绘图 在plot()函数中只有x,y两个量时. import numpy as np import matplotlib.pyplot as plt # 生成曲线上各个点的x,y坐标,然后用一 ...
- matplotlib使用总结
一.简介 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形.通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图 ...
- python 2: 解决python中的plot函数的图例legend不能显示中文问题
问题: 图像标题.横纵坐标轴的标签都能显示中文名字,但是图例就是不能显示中文,怎么解决呢? 解决: plt.figure() plt.title(u'训练性能', fontproperties=f ...
- Python之matplotlib库学习:实现数据可视化
1. 安装和文档 pip install matplotlib 官方文档 为了方便显示图像,还使用了ipython qtconsole方便显示.具体怎么弄网上搜一下就很多教程了. pyplot模块是提 ...
随机推荐
- python笔记4-遍历文件夹目录os.walk()
前言 如何遍历查找出某个文件夹内所有的子文件呢?并且找出某个后缀的所有文件 walk功能简介 1.os.walk() 方法用于通过在目录树种游走输出在目录中的文件名,向上或者向下. 2.walk()方 ...
- 白话空间统计之:Moran's I(莫兰指数)
前两天聊了空间统计学里面的两个经典概念,今天来说说第一篇文章留下的大坑:Moran's I. 首先,Moran's I这个东西.官方叫做:莫兰指数,是澳大利亚统计学家帕特里克·阿尔弗雷德·皮尔斯·莫兰 ...
- 天台人满为患,不如来看下这个Ramnit蠕虫DesktopLayer.exe分析
今年的世界杯越来越看不懂,想去天台吹吹风都不一定有位置,心凉了,事儿还得做,先从网上抓个可疑样本压压惊!上手分析才发现并没有我想得那么简单…… 一.基本信息 MD5 ff5e1f27193ce51ee ...
- LeetCode——3Sum & 3Sum Closest
3Sum 题目 Given an array S of n integers, are there elements a,b,c in S such that a + b + c = 0? Find ...
- How to publish a WordPress blog to a static GitLab Pages site
https://opensource.com/article/18/8/publish-wordpress-static-gitlab-pages-site A long time ago, I se ...
- Spring框架学习(5)spring整合struts2
内容源自:spring整合struts2 一.spring框架对struts等表现层框架的整合原理 : 使用spring的ioc容器管理struts中用于处理请求的Action 将Action配置成i ...
- Thinkpad 笔记本 装win7 64 位操作系统热键驱动装不上问题解决!
Thinkpad 笔记本 装win7 64 位操作系统热键驱动装不上问题解决! 一般牛人的解释如下: 问题终于搞定.是因为需要手工建%PROGRAMFILES%\Lenovo\Hotkey目录.这个写 ...
- 聊聊jvm的PermGen与Metaspace
转载:https://segmentfault.com/a/1190000012577387 序 本文主要讲述一下jvm的PermGen与Metaspace java memory结构 分代概念 对于 ...
- ASP.NET MVC扩展之HtmlHelper辅助方法
什么是HtmlHelper辅助方法? 其实就是HtmlHelper类的扩展方法,如下所示: namespace System.Web.Mvc.Html { public static class Fo ...
- [置顶] 原创鼠标拖动实现DIV排序
先上效果图: 对比传统的排序,这是一个很不错的尝试,希望对大家有启发. 大家可以参考我的上一篇博文:http://blog.csdn.net/littlebo01/article/details/12 ...