关于matplotlib-legend 位置属性 loc 使用说明

 更新时间:2020年05月16日 11:24:55   作者:拦路雨g  
这篇文章主要介绍了关于matplotlib-legend 位置属性 loc 使用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

在使用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)

除'best',另外loc属性有:

'upper right', 'upper left', 'lower left', 'lower right', 'right', 'center left', 'center right', 'lower center', 'upper center', 'center'

不说太多,上面是全部的快捷使用,满足一般需求。

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()

运行结果:

补充知识:设置图列(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 使用说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python3.x爬虫下载网页图片的实例讲解

    Python3.x爬虫下载网页图片的实例讲解

    今天小编就为大家分享一篇Python3.x爬虫下载网页图片的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • python魔法方法之__setattr__()

    python魔法方法之__setattr__()

    这篇文章主要介绍了python魔法方法之__setattr__(),python提供了诸多的魔法方法,其中__setattr__()方法主要用于类实例进行属性赋值,接下来请和小编一起进入文章来了解更多相关内容吧
    2022-03-03
  • 详解MindSpore自定义模型损失函数

    详解MindSpore自定义模型损失函数

    在不同的训练场景中,我们时常需要使用不同的损失函数来衡量一个模型的计算结果的优劣,本文重点介绍了在MindSpore中如何去自定义一个损失函数。基于MindSpore中的Loss类,我们可以通过继承该类后,再重写construct函数和get_loss函数实现全面自定义的损失函数形式与内容
    2021-06-06
  • Django JWT Token RestfulAPI用户认证详解

    Django JWT Token RestfulAPI用户认证详解

    这篇文章主要介绍了Django JWT Token RestfulAPI用户认证详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • python爬取微博评论的实例讲解

    python爬取微博评论的实例讲解

    在本篇文章里小编给大家整理的是一篇关于python爬取微博评论的实例讲解,有兴趣的朋友们可以学习下。
    2021-01-01
  • Python并发编程队列与多线程最快发送http请求方式

    Python并发编程队列与多线程最快发送http请求方式

    假如有一个文件,里面有10万个url,需要对每个url发送http请求,并打印请求结果的状态码,如何编写代码尽可能快的完成这些任务呢
    2021-09-09
  • Python判断和循环语句的分析与应用

    Python判断和循环语句的分析与应用

    判断语句是用来筛选条件,过滤条件的。循环语句是用来解决重复性代码的问题,提高工作效率。今天的知识点不多,耐心看完吧
    2022-07-07
  • python3.6 +tkinter GUI编程 实现界面化的文本处理工具(推荐)

    python3.6 +tkinter GUI编程 实现界面化的文本处理工具(推荐)

    这篇文章主要介绍了python3.6 +tkinter GUI编程 实现界面化的文本处理工具(推荐)的相关资料,需要的朋友可以参考下
    2017-12-12
  • 老生常谈Python startswith()函数与endswith函数

    老生常谈Python startswith()函数与endswith函数

    下面小编就为大家带来一篇老生常谈Python startswith()函数与endswith函数。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • Python 获得命令行参数的方法(推荐)

    Python 获得命令行参数的方法(推荐)

    本篇将介绍python中sys, getopt模块处理命令行参数的方法,本文给大家介绍的非常详细,具有参考借鉴价值,需要的朋友参考下吧
    2018-01-01

最新评论