关于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 BeautifulSoup库抓取58手机维修信息
Nov 21 Python
Python实现爬虫从网络上下载文档的实例代码
Jun 13 Python
Python自定义一个类实现字典dict功能的方法
Jan 19 Python
python实现抖音点赞功能
Apr 07 Python
python basemap 画出经纬度并标定的实例
Jul 09 Python
Python中新式类与经典类的区别详析
Jul 10 Python
Django 1.10以上版本 url 配置注意事项详解
Aug 05 Python
使用Python进行防病毒免杀解析
Dec 13 Python
python中的selenium安装的步骤(浏览器自动化测试框架)
Mar 17 Python
解决 jupyter notebook 回车换两行问题
Apr 15 Python
python里的单引号和双引号的有什么作用
Jun 17 Python
Python中logging日志记录到文件及自动分割的操作代码
Aug 05 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
web方式ftp
2006/10/09 PHP
php in_array 函数使用说明与in_array需要注意的地方说明
2010/04/13 PHP
PHP源码之explode使用说明
2011/08/05 PHP
解析wamp5下虚拟机配置文档
2013/06/27 PHP
Yii中的cookie的发送和读取
2016/07/27 PHP
php抽象方法和抽象类实例分析
2016/12/07 PHP
简单谈谈PHP中的Reload操作
2016/12/12 PHP
js 浮动层菜单收藏
2009/01/16 Javascript
JavaScript 类的定义和引用 JavaScript高级培训 自定义对象
2010/04/27 Javascript
推荐20家国外的脚本下载网站
2011/04/28 Javascript
基于jQuery的前端数据通用验证库
2011/08/08 Javascript
一样的table?不一样的table(可编辑状态table)
2012/09/19 Javascript
关于JS判断图片是否加载完成且获取图片宽度的方法
2013/04/09 Javascript
理解javascript回调函数
2014/12/28 Javascript
Javascript iframe交互并兼容各种浏览器的解决方法
2016/07/12 Javascript
Javascript 6里的4个新语法
2016/08/25 Javascript
javaScript动态添加Li元素的实例
2018/02/24 Javascript
JS实现DOM删除节点操作示例
2018/04/04 Javascript
Vue动态生成表格的行和列
2019/07/18 Javascript
JavaScript 变量,数据类型基础实例详解【变量、字符串、数组、对象等】
2020/01/04 Javascript
vue页面更新patch的实现示例
2020/03/25 Javascript
python实现微信接口(itchat)详细介绍
2017/10/23 Python
python求质数的3种方法
2018/09/28 Python
Python高斯消除矩阵
2019/01/02 Python
Django--权限Permissions的例子
2019/08/28 Python
python 基于wx实现音乐播放
2020/11/24 Python
CSS3模拟动画下拉菜单效果
2017/04/12 HTML / CSS
HTML5 WebGL 实现民航客机飞行监控系统
2019/07/25 HTML / CSS
美国时尚在线:Showpo
2017/09/08 全球购物
娇韵诗Clarins意大利官方网站:法国天然护肤品牌
2020/03/11 全球购物
更夫岗位责任制
2014/02/11 职场文书
普通话宣传标语
2014/06/26 职场文书
2014派出所所长群众路线对照检查材料思想汇报
2014/09/18 职场文书
2014小学生国庆65周年演讲稿
2014/09/21 职场文书
道歉短信大全
2015/05/12 职场文书
复兴之路展览观后感
2015/06/02 职场文书