关于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中比较运算符的使用
May 13 Python
python的keyword模块用法实例分析
Jun 30 Python
Python使用cookielib模块操作cookie的实例教程
Jul 12 Python
Ubuntu 16.04 LTS中源码安装Python 3.6.0的方法教程
Dec 27 Python
Python中文件I/O高效操作处理的技巧分享
Feb 04 Python
Python检测网络延迟的代码
May 15 Python
PyQt5的PyQtGraph实践系列3之实时数据更新绘制图形
May 13 Python
如何使用pyinstaller打包32位的exe程序
May 26 Python
python3.x提取中文的正则表达式示例代码
Jul 23 Python
使用OpCode绕过Python沙箱的方法详解
Sep 03 Python
PyQt5 控件字体样式等设置的实现
May 13 Python
Python如何实现大型数组运算(使用NumPy)
Jul 24 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网上商城购物车设计代码分享
2012/02/15 PHP
PHP实现从远程下载文件的方法
2015/03/12 PHP
Yii使用Captcha验证码的方法
2015/12/28 PHP
laravel 出现command not found问题的解决方案
2019/10/23 PHP
Yii框架模拟组件调用注入示例
2019/11/11 PHP
PHP实现笛卡尔积算法的实例讲解
2019/12/22 PHP
捕获关闭窗口的脚本
2009/01/10 Javascript
JQuery 初体验(建议学习jquery)
2009/04/25 Javascript
JavaScript Chart 插件整理
2010/06/18 Javascript
NodeJS框架Express的模板视图机制分析
2011/07/19 NodeJs
JavaScript设计模式之外观模式介绍
2014/12/28 Javascript
JavaScript实现的字符串replaceAll函数代码分享
2015/04/02 Javascript
JavaScript笔记之数据属性和存储器属性
2016/03/31 Javascript
JS中frameset框架弹出层实例代码
2016/04/01 Javascript
详解BootStrap中Affix控件的使用及保持布局的美观的方法
2016/07/08 Javascript
vue复合组件实现注册表单功能
2017/11/06 Javascript
JavaScript实现修改伪类样式
2017/11/27 Javascript
详解javascript 正则表达式之分组与前瞻匹配
2018/05/30 Javascript
Bootstrap模态对话框用法简单示例
2018/08/31 Javascript
详解express使用vue-router的history踩坑
2019/06/05 Javascript
Vue v-bind动态绑定class实例方法
2020/01/15 Javascript
python使用scrapy解析js示例
2014/01/23 Python
Python3读取文件常用方法实例分析
2015/05/22 Python
flask中的wtforms使用方法
2018/07/21 Python
对python中assert、isinstance的用法详解
2019/11/27 Python
python装饰器使用实例详解
2019/12/14 Python
python GUI库图形界面开发之PyQt5 UI主线程与耗时线程分离详细方法实例
2020/02/26 Python
python 追踪except信息方式
2020/04/25 Python
Boden澳大利亚官网:英国在线服装公司
2018/08/05 全球购物
苹果台湾官网:Apple台湾
2019/01/05 全球购物
医院辞职信范文
2014/01/17 职场文书
乡镇庆八一活动方案
2014/02/02 职场文书
2015年小学图书室工作总结
2015/05/18 职场文书
一波干货,会议主持词开场白范文
2019/05/06 职场文书
Python上下文管理器Content Manager
2021/06/26 Python
Oracle数据库事务的开启与结束详解
2022/06/25 Oracle