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之有容乃大的list(2)
Sep 15 Python
详解Python中的join()函数的用法
Apr 07 Python
Python解析树及树的遍历
Feb 03 Python
Python对字符串实现去重操作的方法示例
Aug 11 Python
Python基于回溯法子集树模板解决野人与传教士问题示例
Sep 11 Python
Python实现按照指定要求逆序输出一个数字的方法
Apr 19 Python
详解Python基础random模块随机数的生成
Mar 23 Python
树莓派使用USB摄像头和motion实现监控
Jun 22 Python
python科学计算之narray对象用法
Nov 25 Python
python 函数中的参数类型
Feb 11 Python
Python 实现简单的客户端认证
Jul 29 Python
Python 带星号(* 或 **)的函数参数详解
Feb 23 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中的时间处理
2006/10/09 PHP
在PHP中使用redis
2013/11/04 PHP
详解WordPress开发中过滤属性以及Sql语句的函数使用
2015/12/25 PHP
使用tp框架和SQL语句查询数据表中的某字段包含某值
2019/10/18 PHP
自写简单JS判断是否已经弹出页面
2010/10/20 Javascript
基于Jquery的回车成tab焦点切换效果代码(Enter To Tab )
2010/11/14 Javascript
js下通过prototype扩展实现indexOf的代码
2010/12/08 Javascript
jQuery 对Select的操作备忘记录
2011/07/04 Javascript
js 时间格式与时间戳的相互转换示例代码
2013/12/25 Javascript
node.js使用cluster实现多进程
2016/03/17 Javascript
JS仿百度自动下拉框模糊匹配提示
2016/07/25 Javascript
最丑的时钟效果!js canvas时钟制作方法
2016/08/15 Javascript
jquery radio的取值_radio的选中_radio的重置方法
2016/09/20 Javascript
Vue学习笔记进阶篇之函数化组件解析
2017/07/21 Javascript
windows下更新npm和node的方法
2017/11/30 Javascript
js实现以最简单的方式将数组元素添加到对象中的方法
2017/12/20 Javascript
WebSocket的通信过程与实现方法详解
2018/04/29 Javascript
JavaScript作用域、闭包、对象与原型链概念及用法实例总结
2018/08/20 Javascript
浅谈vue同一页面中拥有两个表单时,的验证问题
2018/09/18 Javascript
Vue中的Props(不可变状态)
2018/09/29 Javascript
JavaScript中0、空字符串、'0'是true还是false的知识点分享
2019/09/16 Javascript
Python的网络编程库Gevent的安装及使用技巧
2016/06/24 Python
Python使用poplib模块和smtplib模块收发电子邮件的教程
2016/07/02 Python
详解Python 数据库的Connection、Cursor两大对象
2018/06/25 Python
奥地利顶级内衣丝袜品牌英国站:Wolford英国
2016/08/29 全球购物
荷兰街头时尚之家:Funkie House
2019/03/18 全球购物
JPA面试常见问题
2016/11/14 面试题
杭州龙健科技笔试题.net部分笔试题
2016/01/24 面试题
GWT (Google Web Toolkit)有哪些主要的原件组成?
2015/06/08 面试题
交通事故赔偿协议书
2014/04/15 职场文书
留守儿童工作方案
2014/06/02 职场文书
大专生找工作自荐书
2014/06/10 职场文书
企业安全生产责任书范本
2014/07/28 职场文书
材料物理专业求职信
2014/09/01 职场文书
《水上飞机》教学反思
2016/02/20 职场文书
《岳阳楼记》原文、译文赏析
2019/09/10 职场文书