关于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 27 Python
Python中字典的基本知识初步介绍
May 21 Python
Python中的默认参数详解
Jun 24 Python
python+selenium实现京东自动登录及秒杀功能
Nov 18 Python
详解python while 函数及while和for的区别
Sep 07 Python
新年快乐! python实现绚烂的烟花绽放效果
Jan 30 Python
python将字符串转换成json的方法小结
Jul 09 Python
python 利用jinja2模板生成html代码实例
Oct 10 Python
python修改linux中文件(文件夹)的权限属性操作
Mar 05 Python
python高阶函数map()和reduce()实例解析
Mar 16 Python
Softmax函数原理及Python实现过程解析
May 22 Python
使用Numpy对特征中的异常值进行替换及条件替换方式
Jun 08 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/11/25 PHP
PHP+AJAX实现无刷新注册(带用户名实时检测)
2007/01/02 PHP
PHP中$_SERVER的详细参数与说明介绍
2013/10/26 PHP
php实现监听事件
2013/11/06 PHP
网站防止被刷票的一些思路与方法
2015/01/08 PHP
PHP实现通用alert函数的方法
2015/03/11 PHP
PHP实现即时输出、实时输出内容方法
2015/05/27 PHP
Apache服务器下防止图片盗链的办法
2015/07/06 PHP
php将服务端的文件读出来显示在web页面实例
2016/10/31 PHP
php 微信开发获取用户信息如何实现
2016/12/13 PHP
phpQuery采集网页实现代码实例
2020/04/02 PHP
一个js写的日历(代码部分网摘)
2009/09/20 Javascript
利用JS进行图片的切换即特效展示图片
2013/12/03 Javascript
JavaScript中的函数声明和函数表达式区别浅析
2015/03/27 Javascript
AngularJS基础 ng-hide 指令用法及示例代码
2016/08/01 Javascript
bootstrap按钮插件(Button)使用方法解析
2017/01/13 Javascript
Javascript Event(事件)的传播与冒泡
2017/01/23 Javascript
详解利用 Vue.js 实现前后端分离的RBAC角色权限管理
2017/09/15 Javascript
详解JavaScript的内存空间、赋值和深浅拷贝
2019/04/17 Javascript
uni-app自定义导航栏按钮|uniapp仿微信顶部导航条功能
2019/11/12 Javascript
jQuery实现的解析本地 XML 文档操作示例
2020/04/30 jQuery
vue + node如何通过一个Txt文件批量生成MP3并压缩成Zip
2020/06/02 Javascript
JavaScript 实现轮播图特效的示例
2020/11/05 Javascript
[01:25]DOTA2超级联赛专访iG 将调整状态找回自己
2013/06/05 DOTA
Python日期操作学习笔记
2008/10/07 Python
状态机的概念和在Python下使用状态机的教程
2015/04/11 Python
Python内建模块struct实例详解
2018/02/02 Python
python使用Paramiko模块实现远程文件拷贝
2019/04/30 Python
python用WxPython库实现无边框窗体和透明窗体实现方法详解
2020/02/21 Python
html5 利用canvas手写签名并保存的实现方法
2018/07/12 HTML / CSS
html5表单及新增的改良元素详解
2016/06/07 HTML / CSS
市场营销毕业生自荐信
2013/11/23 职场文书
MATLAB 全景图切割及盒图显示的实现步骤
2021/05/14 Python
关于Spring配置文件加载方式变化引发的异常详解
2022/01/18 Java/Android
移除Selenium中window.navigator.webdriver值
2022/06/10 Python
使用Cargo工具高效创建Rust项目
2022/08/14 Javascript