关于matplotlib-legend 位置属性 loc 使用说明


Posted in Python onMay 16, 2020

在使用matplotlib画图时,少不了对性能图形做出一些说明和补充。一般情况下,loc属性设置为'best'就足够应付了

plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 'best')

或直接loc = 0

plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 0)

关于matplotlib-legend 位置属性 loc 使用说明

除'best',另外loc属性有:

'upper right', 'upper left', 'lower left', 'lower right', 'right', 'center left', 'center right', 'lower center', 'upper center', 'center'

关于matplotlib-legend 位置属性 loc 使用说明

不说太多,上面是全部的快捷使用,满足一般需求。

demo:

import matplotlib.pyplot as plt
import numpy as np
 
# 绘制普通图像
x = np.linspace(-1, 1, 50)
y1 = 2 * x + 1
y2 = x**2
 
plt.figure()
# 在绘制时设置lable, 逗号是必须的
l1, = plt.plot(x, y1, label = 'line')
l2, = plt.plot(x, y2, label = 'parabola', color = 'red', linewidth = 1.0, linestyle = '--')
 
# 设置坐标轴的取值范围
plt.xlim((-1, 1))
plt.ylim((0, 2))
 
# 设置坐标轴的lable
plt.xlabel('X axis')
plt.ylabel('Y axis')
 
# 设置x坐标轴刻度, 原来为0.25, 修改后为0.5
plt.xticks(np.linspace(-1, 1, 5))
# 设置y坐标轴刻度及标签, $$是设置字体
plt.yticks([0, 0.5], ['$minimum$', 'normal'])
 
# 设置legend
plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 'best')
plt.show()

运行结果:

关于matplotlib-legend 位置属性 loc 使用说明

补充知识:设置图列(key/legend)的位置和大小 --gnuplot

先看几个例子:

//不显示图例。
unset key
//设置图例 显示在图形(内)的顶部居中,并且多个图例水平显示。
set key top horizontal center
//设置图例 显示在图形(外)的顶部居中,并且多个图例水平显示。
set key top outside horizontal center
//设置图例 显示的字体并加粗。
set key font "Times,18,Bold"
//调整图例行间隔
set key spacing 3
//调整图例中线段示例长度
set key samplen 2

set key 的语法规则

Syntax: 
   set key {on|off} {default}
       {{inside | outside} | {lmargin | rmargin | tmargin | bmargin}
        | {at <position>}}
       {left | right | center} {top | bottom | center}
       {vertical | horizontal} {Left | Right}
       {{no}reverse} {{no}invert}
       {samplen <sample_length>} {spacing <vertical_spacing>}
       {width <width_increment>}
       {height <height_increment>}
       {{no}autotitle {columnheader}}
       {title "<text>"} {{no}enhanced}
       {{no}box { {linestyle | ls <line_style>}
            | {linetype | lt <line_type>}
             {linewidth | lw <line_width>}}}
   unset key
   show key

Elements within the key are stacked according to vertical or horizontal. In the case of vertical, the key occupies as few columns as possible. That is, elements are aligned in a column until running out of vertical space at which point a new column is started. In the case of horizontal, the key occupies as few rows as possible.

图例是依据我们设置的水平显示或垂直显示进行堆叠式地显示。

对于垂直显示,pnuplot会占用尽可能少的行来放置我们的图例,当图例在一行显示不下时,它会另启一行来显示。

对于水平显示方式,pnuplot会占用尽可能少的列来放置我们的图例,当图例在一列显示不下时,它会另启一列来放置。

The vertical spacing between lines is controlled by spacing. The spacing is set equal to the product of the pointsize, the vertical tic size, and vertical_spacing. The program will guarantee that the vertical spacing is no smaller than the character height.

The defaults for set key are on, right, top, vertical, Right, noreverse, noinvert, samplen 4, spacing 1.25, title “”, and nobox.

以上这篇关于matplotlib-legend 位置属性 loc 使用说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实现的使用telnet登陆聊天室实例
Jun 17 Python
python实现用户登陆邮件通知的方法
Jul 09 Python
Python验证企业工商注册码
Oct 25 Python
Python中easy_install 和 pip 的安装及使用
Jun 05 Python
Python实现一个简单的验证码程序
Nov 03 Python
Python 找到列表中满足某些条件的元素方法
Jun 26 Python
Python 3.3实现计算两个日期间隔秒数/天数的方法示例
Jan 07 Python
python通过配置文件共享全局变量的实例
Jan 11 Python
Python3几个常见问题的处理方法
Feb 26 Python
用Python写一个模拟qq聊天小程序的代码实例
Mar 06 Python
解决Pyinstaller 打包exe文件 取消dos窗口(黑框框)的问题
Jun 21 Python
python_matplotlib改变横坐标和纵坐标上的刻度(ticks)方式
May 16 Python
Python matplotlib画图时图例说明(legend)放到图像外侧详解
May 16 #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
You might like
生成php程序的php代码
2008/04/07 PHP
php实现的一个很好用HTML解析器类可用于采集数据
2013/09/23 PHP
PHP批量检测并去除文件BOM头代码实例
2014/05/08 PHP
PHP生成条形图的方法
2014/12/10 PHP
PHPExcel导出2003和2007的excel文档功能示例
2017/01/04 PHP
对于Laravel 5.5核心架构的深入理解
2018/02/22 PHP
javascript 特性检测并非浏览器检测
2010/01/15 Javascript
兼容IE、FireFox、Chrome等浏览器的xml处理函数js代码
2011/11/30 Javascript
jQuery中的height innerHeight outerHeight区别示例介绍
2014/06/15 Javascript
Bootstrap选项卡动态切换效果
2016/11/28 Javascript
javascript基本数据类型和转换
2017/03/17 Javascript
JS 组件系列之BootstrapTable的treegrid功能
2017/06/16 Javascript
理解 Node.js 事件驱动机制的原理
2017/08/16 Javascript
JavaScript创建防篡改对象的方法分析
2018/12/30 Javascript
js HTML DOM EventListener功能与用法实例分析
2020/04/27 Javascript
vue+elementui实现点击table中的单元格触发事件--弹框
2020/07/18 Javascript
vue缓存之keep-alive的理解和应用详解
2020/11/02 Javascript
在Django的上下文中设置变量的方法
2015/07/20 Python
使用Python编写简单的画图板程序的示例教程
2015/12/08 Python
Python3 处理JSON的实例详解
2017/10/29 Python
Python谱减法语音降噪实例
2019/12/18 Python
利用Tensorflow构建和训练自己的CNN来做简单的验证码识别方式
2020/01/20 Python
HTML5适合的情人节礼物有纪念日期功能
2021/01/25 HTML / CSS
乌克兰香水和化妆品网站:Notino.ua
2018/03/26 全球购物
澳大利亚百货商店中销量第一的商务衬衫品牌:Van Heusen
2018/07/26 全球购物
机械专业应届生求职信
2013/12/12 职场文书
营销人才自我鉴定范文
2013/12/25 职场文书
12月红领巾广播稿
2014/02/13 职场文书
2014村务公开实施方案
2014/02/25 职场文书
商学院大学生求职的自我评价
2014/03/12 职场文书
信息与计算机科学职业规划范文:成为一艘有方向的船
2014/09/11 职场文书
大学班干部竞选稿
2015/11/20 职场文书
生活委员竞选稿
2015/11/21 职场文书
敬业奉献模范事迹材料(2016精选版)
2016/02/26 职场文书
CSS3 制作的书本翻页特效
2021/04/13 HTML / CSS
自定义函数实现单词排序并运用于PostgreSQL(实现代码)
2021/04/22 PostgreSQL