关于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中几个常用的类方法
Apr 08 Python
使用70行Python代码实现一个递归下降解析器的教程
Apr 17 Python
Python中json格式数据的编码与解码方法详解
Jul 01 Python
python使用xlrd与xlwt对excel的读写和格式设定
Jan 21 Python
Python在图片中添加文字的两种方法
Apr 29 Python
Python subprocess模块详细解读
Jan 29 Python
Python管理Windows服务小脚本
Mar 12 Python
实例讲解Python爬取网页数据
Jul 08 Python
python 返回列表中某个值的索引方法
Nov 07 Python
selenium+python自动化测试之鼠标和键盘事件
Jan 23 Python
python 获取毫秒数,计算调用时长的方法
Feb 20 Python
set在python里的含义和用法
Jun 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
星际争霸任务指南——人族
2020/03/04 星际争霸
基于Linux调试工具strace与gdb的常用命令总结
2013/06/03 PHP
Zend Framework动作助手(Zend_Controller_Action_Helper)用法详解
2016/03/05 PHP
php面向对象编程self和static的区别
2016/05/08 PHP
Yii2 中实现单点登录的方法
2018/03/09 PHP
微信公众平台开发教程①获取用户Openid及个人信息图文详解
2019/04/10 PHP
js渐变显示渐变消失示例代码
2013/08/01 Javascript
自定义jQuery插件方式实现强制对象重绘的方法
2015/03/23 Javascript
javascript显示上周、上个月日期的处理方法
2016/02/03 Javascript
原生JS轮播图插件
2017/02/09 Javascript
JS实现网页抢购功能(触发,终止脚本)
2017/11/27 Javascript
vue-cli中vue本地实现跨域调试接口
2019/01/16 Javascript
使用js在layui中实现上传图片压缩
2019/06/18 Javascript
vue3修改link标签默认icon无效问题详解
2019/10/09 Javascript
vue使用recorder.js实现录音功能
2019/11/22 Javascript
Vue实现计算器计算效果
2020/08/17 Javascript
全面解析Vue中的$nextTick
2020/12/24 Vue.js
[00:34]拔城逐梦,热血永恒!2020(秋)完美世界城市挑战赛报名开启
2020/10/09 DOTA
python编辑用户登入界面的实现代码
2018/07/16 Python
Python爬虫之pandas基本安装与使用方法示例
2018/08/08 Python
一篇文章搞懂Python的类与对象名称空间
2018/12/10 Python
Python基于mysql实现学生管理系统
2019/02/21 Python
Django框架实现分页显示内容的方法详解
2019/05/10 Python
使用Python判断一个文件是否被占用的方法教程
2020/12/16 Python
Skechers越南官方网站:来自美国的运动休闲品牌
2021/02/22 全球购物
管理心得体会
2013/12/28 职场文书
学校卫生检查制度
2014/02/03 职场文书
电气自动化专业职业规划范文
2014/02/16 职场文书
求职信怎么写范文
2014/05/26 职场文书
活动总结结尾怎么写
2014/08/30 职场文书
2015年度酒店客房部工作总结
2015/05/25 职场文书
运动会通讯稿300字
2015/07/20 职场文书
初一年级组工作总结
2015/08/12 职场文书
大学生饮品店创业计划书范文
2019/07/10 职场文书
Python基础之Socket通信原理
2021/04/22 Python
SpringBoot深入分析讲解监听器模式下
2022/07/15 Java/Android