关于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 相关文章推荐
Python3中的真除和Floor除法用法分析
Mar 16 Python
python实现批量监控网站
Sep 09 Python
利用Python脚本生成sitemap.xml的实现方法
Jan 31 Python
python实现解数独程序代码
Apr 12 Python
Python元组操作实例分析【创建、赋值、更新、删除等】
Jul 24 Python
Django自定义用户认证示例详解
Mar 14 Python
python实现支付宝当面付(扫码支付)功能
May 30 Python
Python3 实现串口两进程同时读写
Jun 12 Python
Python Web框架之Django框架cookie和session用法分析
Aug 16 Python
python由已知数组快速生成新数组的方法
Apr 08 Python
Django日志及中间件模块应用案例
Sep 10 Python
Python机器学习之逻辑回归
May 11 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操作MongoDB时的整数问题及对策说明
2011/05/02 PHP
php采用curl访问域名返回405 method not allowed提示的解决方法
2014/06/26 PHP
Yii 快速,安全,专业的PHP框架
2014/09/03 PHP
PHP实现的sqlite数据库连接类
2014/12/12 PHP
简单谈谈php延迟静态绑定
2016/01/26 PHP
PHP自定义错误处理的方法分析
2018/12/19 PHP
javascript基础第一章 JavaScript与用户端
2010/07/22 Javascript
jQuery+CSS 实现随滚动条增减的汽水瓶中的液体效果
2011/09/26 Javascript
Google Dart编程语法和基本类型学习教程
2013/11/27 Javascript
jQuery中offsetParent()方法用法实例
2015/01/19 Javascript
javascript中Date format(js日期格式化)方法小结
2015/12/17 Javascript
jQuery插件FusionCharts绘制2D柱状图和折线图的组合图效果示例【附demo源码】
2017/04/10 jQuery
vue-cli如何添加less 以及sass
2017/07/06 Javascript
[js高手之路]从原型链开始图解继承到组合继承的产生详解
2017/08/28 Javascript
解决Mac安装thrift因bison报错的问题
2018/05/17 Javascript
微信小程序 简易计算器实现代码实例
2019/09/02 Javascript
vue实现商城秒杀倒计时功能
2019/12/12 Javascript
javascript实现扫雷简易版
2020/08/18 Javascript
使用简单工厂模式来进行Python的设计模式编程
2016/03/01 Python
浅谈django model的get和filter方法的区别(必看篇)
2017/05/23 Python
Python cookbook(数据结构与算法)让字典保持有序的方法
2018/02/18 Python
Python requests发送post请求的一些疑点
2018/05/20 Python
pandas DataFrame索引行列的实现
2019/06/04 Python
处理Selenium3+python3定位鼠标悬停才显示的元素
2019/07/31 Python
Python3 main函数使用sys.argv传入多个参数的实现
2019/12/25 Python
python获取系统内存占用信息的实例方法
2020/07/17 Python
怎么解决pycharm license Acti的方法
2020/10/28 Python
什么是反射?如何实现反射?
2016/07/25 面试题
临床医学专业学生的自我评价分享
2013/11/21 职场文书
采购主管岗位职责
2014/02/01 职场文书
小学美术兴趣小组活动总结
2014/07/07 职场文书
申报优秀教师材料
2014/12/16 职场文书
就业证明函
2015/06/17 职场文书
网吧管理制度范本
2015/08/05 职场文书
法律服务所工作总结
2015/08/10 职场文书
三年级作文之趣事作文
2019/11/04 职场文书