关于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中map()与zip()操作方法
Feb 27 Python
Django 忘记管理员或忘记管理员密码 重设登录密码的方法
May 30 Python
python3 拼接字符串的7种方法
Sep 12 Python
win7 x64系统中安装Scrapy的方法
Nov 18 Python
python pandas时序处理相关功能详解
Jul 03 Python
Flask配置Cors跨域的实现
Jul 12 Python
利用Python进行图像的加法,图像混合(附代码)
Jul 14 Python
Python使用Opencv实现图像特征检测与匹配的方法
Oct 30 Python
python pyenv多版本管理工具的使用
Dec 23 Python
Python 中由 yield 实现异步操作
May 04 Python
Python爬虫+tkinter界面实现历史天气查询的思路详解
Feb 22 Python
Python语言内置数据类型
Feb 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
全国FM电台频率大全 - 27 陕西省
2020/03/11 无线电
网络资源
2006/10/09 PHP
PHP 一个页面执行时间类代码
2010/03/05 PHP
PHP 登录完成后如何跳转上一访问页面
2014/01/14 PHP
使用PHP+MySql实现微信投票功能实例代码
2017/09/29 PHP
浅谈使用 Yii2 AssetBundle 中 $publishOptions 的正确姿势
2017/11/08 PHP
JS 控制非法字符的输入代码
2009/12/04 Javascript
从盛大通行证上摘下来的身份证验证js代码
2011/01/11 Javascript
JS获取并操作iframe中元素的方法
2013/03/21 Javascript
实现图片预加载的三大方法及优缺点分析
2014/11/19 Javascript
JS实现简单的图书馆享元模式实例
2015/06/30 Javascript
使用JavaScript进行表单校验功能
2017/08/01 Javascript
js匿名函数使用&amp;传参(实例)
2017/09/08 Javascript
vue.js,ajax渲染页面的实例
2018/02/11 Javascript
JavaScript面向对象程序设计创建对象的方法分析
2018/08/13 Javascript
vue+canvas实现炫酷时钟效果的倒计时插件(已发布到npm的vue2插件,开箱即用)
2018/11/05 Javascript
Vue 路由间跳转和新开窗口的方式(query、params)
2019/12/25 Javascript
vue中使用vue-print.js实现多页打印
2020/03/05 Javascript
ES6学习教程之Promise用法详解
2020/11/22 Javascript
python3.x 将byte转成字符串的方法
2018/07/17 Python
pygame实现俄罗斯方块游戏(AI篇1)
2019/10/29 Python
tensorflow 实现从checkpoint中获取graph信息
2020/02/10 Python
python与c语言的语法有哪些不一样的
2020/09/13 Python
python中HTMLParser模块知识点总结
2021/01/25 Python
《白鹅》教学反思
2014/04/13 职场文书
党员承诺书范文
2014/05/19 职场文书
力学专业求职信
2014/07/23 职场文书
涨价通知
2015/04/23 职场文书
你会写请假条吗?
2019/06/26 职场文书
2019最新婚庆对联集锦!
2019/07/10 职场文书
OpenCV-Python模板匹配人眼的实例
2021/06/08 Python
HTML怎么设置下划线?html文字加下划线方法
2021/12/06 HTML / CSS
Spring Boot实战解决高并发数据入库之 Redis 缓存+MySQL 批量入库问题
2022/02/12 Redis
上个世纪50年代的可穿戴技术:无线电帽子
2022/02/18 无线电
《乙女游戏世界对路人角色很不友好》OP主题曲无字幕动画MV公开
2022/04/05 日漫
MySQL 数据表操作
2022/05/04 MySQL