关于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实现SMTP发送邮件详细教程
Mar 02 Python
Flask框架的学习指南之开发环境搭建
Nov 20 Python
python删除不需要的python文件方法
Apr 24 Python
快速解决pandas.read_csv()乱码的问题
Jun 15 Python
Python编程深度学习计算库之numpy
Dec 28 Python
pyinstaller打包多个py文件和去除cmd黑框的方法
Jun 21 Python
详解在Python中以绝对路径或者相对路径导入文件的方法
Aug 30 Python
python的命名规则知识点总结
Oct 04 Python
python+tifffile之tiff文件读写方式
Jan 13 Python
Python 私有属性和私有方法应用场景分析
Jun 19 Python
Python3爬虫发送请求的知识点实例
Jul 30 Python
5道关于python基础 while循环练习题
Nov 27 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中的时间处理
2006/10/09 PHP
php中使用Akismet防止垃圾评论的代码
2011/06/10 PHP
php抽奖小程序的实现代码
2013/06/18 PHP
PHP中time(),date(),mktime()区别介绍
2013/09/28 PHP
分享PHP计算两个日期相差天数的代码
2015/12/23 PHP
PHP闭包函数详解
2016/02/13 PHP
PHP递归遍历多维数组实现无限分类的方法
2016/05/06 PHP
PHP 无限级分类
2017/05/04 PHP
PHP实现正则表达式分组捕获操作示例
2018/02/03 PHP
一段非常简单的让图片自动切换js代码
2006/11/10 Javascript
jquery 学习笔记 传智博客佟老师附详细注释
2020/09/12 Javascript
基于jQuery的ajax功能实现web service的json转化
2009/08/29 Javascript
JS操作Cookies的小例子
2013/10/15 Javascript
js 阻止子元素响应父元素的onmouseout事件具体实现
2013/12/23 Javascript
JavaScript数组深拷贝和浅拷贝的两种方法
2014/04/16 Javascript
jQuery Easyui datagrid/treegrid 清空数据
2016/07/09 Javascript
JavaScript获取css行间样式,内连样式和外链样式的简单方法
2016/07/18 Javascript
NodeJS处理Express中异步错误
2017/03/26 NodeJs
Vue 组件(component)教程之实现精美的日历方法示例
2018/01/08 Javascript
vue 组件高级用法实例详解
2018/04/11 Javascript
webpack 从指定入口文件中提取公共文件的方法
2018/11/13 Javascript
Javascript Worker子线程代码实例
2020/02/20 Javascript
python解析xml文件实例分析
2015/05/27 Python
matplotlib设置legend图例代码示例
2017/12/19 Python
python爬虫_实现校园网自动重连脚本的教程
2018/04/22 Python
Python 中的range(),以及列表切片方法
2018/07/02 Python
详解Django中间件的5种自定义方法
2018/07/26 Python
Python如何实现动态数组
2019/11/02 Python
python中sympy库求常微分方程的用法
2020/04/28 Python
基于CSS3实现的几个小loading效果
2018/09/27 HTML / CSS
CSS3实现闪烁动画效果的方法
2015/02/09 HTML / CSS
秘书岗位职责
2013/11/18 职场文书
经典优秀个人求职信分享
2013/12/12 职场文书
党员群众路线对照检查材料
2014/08/31 职场文书
2015驻村干部工作总结
2015/04/07 职场文书
电影地道战观后感
2015/06/04 职场文书