Python matplotlib画图时图例说明(legend)放到图像外侧详解


Posted in Python onMay 16, 2020

用python的matplotlib画图时,往往需要加图例说明。如果不设置任何参数,默认是加到图像的内侧的最佳位置。

import matplotlib.pyplot as plt
import numpy as np
 
x = np.arange(10)
 
fig = plt.figure()
ax = plt.subplot(111)
 
for i in xrange(5):
 ax.plot(x, i * x, label='$y = %ix$' % i)
 
plt.legend()
 
plt.show()

这样的结果如图所示:

Python matplotlib画图时图例说明(legend)放到图像外侧详解

如果需要将该legend移到图像外侧,有多种方法,这里介绍一种。

在plt.legend()函数中加入若干参数:

plt.legend(bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4)

bbox_to_anchor(num1,num2)表示legend的位置和图像的位置关系,num1表示水平位置,num2表示垂直位置。num1=0表示legend位于图像的左侧垂直线(这里的其它参数设置:num2=0,num3=3,num4=0)。

Python matplotlib画图时图例说明(legend)放到图像外侧详解

num1=1表示legend位于图像的右侧垂直线(其它参数设置:num2=0,num3=3,num4=0)。

Python matplotlib画图时图例说明(legend)放到图像外侧详解

为了美观,需要将legend放于图像的外侧,而又距离不是太大,一般设num1=1.05。

num2=0表示legend位于图像下侧水平线(其它参数设置:num1=1.05,num3=3,num4=0)。

Python matplotlib画图时图例说明(legend)放到图像外侧详解

num2=1表示legend位于图像上侧水平线(其它参数设置:num1=1.05,num3=3,num4=0)。

Python matplotlib画图时图例说明(legend)放到图像外侧详解

所以,如果希望legend位于图像的右下,需要将num2设为0,位于图像的右上,需要将num2设为1。

由于legend是一个方框,bbox_to_anchor=(num1, num2)相当于表示一个点,那么legend的哪个位置位于这个点上呢。参数num3就用以表示哪个位置位于该点。

loc参数对应
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

所以,当设bbox_to_anchor=(1.05,0),即legend放于图像右下角时,为美观起见,需要将legend的左下角,即'lower left'放置该点,对应该表的‘Location Code'数字为3,即参数num3置为3或直接设为‘lower left';而当设bbox_to_anchor=(1.05,1),即legend放于图像右上角时,为美观起见,需要将legend的左上角,即'upper left'放置该点,对应该表的‘Location Code'数字为2,即参数num3置为2或直接设为‘upper left'。

根据参考网址上的解释,参数num4表示轴和legend之间的填充,以字体大小距离测量,默认值为None,但实际操作中,如果不加该参数,效果是有一定的填充,下面有例图展示,我这里设为0,即取消填充,具体看个人选择。

这是将legend放于图像右下的完整代码:

import matplotlib.pyplot as plt
import numpy as np
 
x = np.arange(10)
 
fig = plt.figure()
ax = plt.subplot(111)
 
for i in xrange(5):
 ax.plot(x, i * x, label='$y = %ix$' % i)
 
plt.legend(bbox_to_anchor=(1.05, 0), loc=3, borderaxespad=0)
 
plt.show()

效果展示:

Python matplotlib画图时图例说明(legend)放到图像外侧详解

这里legend的‘lower left'置于(1.05, 0)的位置。

如果不加入参数num4,那么效果为:

Python matplotlib画图时图例说明(legend)放到图像外侧详解

legend稍靠上,有一定的填充。

这是将legend放于图像右上的完整代码:

import matplotlib.pyplot as plt
import numpy as np
 
x = np.arange(10)
 
fig = plt.figure()
ax = plt.subplot(111)
 
for i in xrange(5):
 ax.plot(x, i * x, label='$y = %ix$' % i)
 
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)
 
plt.show()

效果展示:

Python matplotlib画图时图例说明(legend)放到图像外侧详解

这里legend的‘upper left'置于(1.05, 0)的位置。

如果不加入参数num4,那么效果为:

Python matplotlib画图时图例说明(legend)放到图像外侧详解

legend稍靠下。

以上这篇Python matplotlib画图时图例说明(legend)放到图像外侧详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python二叉树的实现实例
Nov 21 Python
gearman的安装启动及python API使用实例
Jul 08 Python
python实现带声音的摩斯码翻译实现方法
May 20 Python
Python实现比较两个列表(list)范围
Jun 12 Python
在Ubuntu系统下安装使用Python的GUI工具wxPython
Feb 18 Python
Python处理json字符串转化为字典的简单实现
Jul 07 Python
Python实现可获取网易页面所有文本信息的网易网络爬虫功能示例
Jan 15 Python
浅谈Pycharm中的Python Console与Terminal
Jan 17 Python
python requests指定出口ip的例子
Jul 25 Python
Pytorch使用MNIST数据集实现基础GAN和DCGAN详解
Jan 10 Python
python字典的值可以修改吗
Jun 29 Python
深入理解python协程
Jun 15 Python
python_matplotlib改变横坐标和纵坐标上的刻度(ticks)方式
May 16 #Python
使用Python matplotlib作图时,设置横纵坐标轴数值以百分比(%)显示
May 16 #Python
Python验证码截取识别代码实例
May 16 #Python
基于plt.title无法显示中文的快速解决
May 16 #Python
基于python生成英文版词云图代码实例
May 16 #Python
解决Python数据可视化中文部分显示方块问题
May 16 #Python
Python使用Excel将数据写入多个sheet
May 16 #Python
You might like
基于PHP常用函数的用法详解
2013/05/10 PHP
codeigniter数据库操作函数汇总
2014/06/12 PHP
php获取当前月与上个月月初及月末时间戳的方法
2016/12/05 PHP
yii框架数据库关联查询操作示例
2019/10/14 PHP
使用基于jquery的gamequery插件做JS乒乓球游戏
2011/07/31 Javascript
一个关于jqGrid使用的小例子(行按钮)
2011/11/04 Javascript
使用js操作cookie的一点小收获分享
2013/09/03 Javascript
简单的js图片轮换代码(js图片轮播)
2014/05/06 Javascript
javascript与css3动画结合使用小结
2015/03/11 Javascript
Javascript中Array用法实例分析
2015/06/13 Javascript
jQuery的实例及必知重要的jQuery选择器详解
2016/05/20 Javascript
JS实现图片上传预览功能
2016/11/21 Javascript
vue的Virtual Dom实现snabbdom解密
2017/05/03 Javascript
vue2.0 computed 计算list循环后累加值的实例
2018/03/07 Javascript
vue.js中ref和$refs的使用及示例讲解
2019/08/14 Javascript
python的id()函数介绍
2013/02/10 Python
Python写的英文字符大小写转换代码示例
2015/03/06 Python
python 循环while和for in简单实例
2016/08/16 Python
TensorFlow实现创建分类器
2018/02/06 Python
Python利用正则表达式实现计算器算法思路解析
2018/04/25 Python
python多进程提取处理大量文本的关键词方法
2018/06/05 Python
python读取目录下最新的文件夹方法
2018/12/24 Python
python如何以表格形式打印输出的方法示例
2019/06/21 Python
Python数据分析模块pandas用法详解
2019/09/04 Python
Python调用Windows命令打印文件
2020/02/07 Python
Python Flask上下文管理机制实例解析
2020/03/16 Python
荷兰皇家航空公司官方网站:KLM Royal Dutch Airlines
2017/12/07 全球购物
欧洲最大的拼图游戏商店:JigsawPuzzle.co.uk
2018/07/04 全球购物
Hawes & Curtis官网:英国经典品牌
2019/07/27 全球购物
好矿嫂事迹材料
2014/01/21 职场文书
安全资金保障制度
2014/01/23 职场文书
岗位职责风险点
2014/03/12 职场文书
销售个人求职信范文
2014/04/28 职场文书
罗马假日观后感
2015/06/08 职场文书
搞笑的婚礼主持词
2015/06/29 职场文书
webpack介绍使用配置教程详解webpack介绍和使用
2022/06/25 Javascript