关于matplotlib-legend 位置属性 loc 使用说明
作者:拦路雨g 发布时间:2021-12-19 06:48:25
在使用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.
来源:https://blog.csdn.net/lanluyug/article/details/80002273


猜你喜欢
- 1-错误详情cmd下运行net start mysql启动MySQL服务:提示发生系统错误5。拒绝访问。cmd管理员模式运行可以启动MySQ
- Rex 是 Perl 编写的基于 SSH 链接的集群配置管理系统,语法上类似 Puppet DSL。官网中文版见 http://rex.pe
- 通常情况下,定义函数时都会选择有参数的函数形式,函数参数的作用是传递数据给函数,令其对接收的数据做具体的操作处理。在使用函数时,经常会用到形
- 著名的老掉牙的IE6.0在我这里已经有六年工龄了,前几天朋友拿到个IE8.0新的Beta版本,我的Sever2003装不上,大为扫兴。Chr
- 今天分享的这篇文章,文字不多,代码为主。绝对干货,童叟无欺,主要分享了提升 Python 性能的 20 个技巧,教你如何告别慢Python。
- HTML 标签 textarea 在大部分浏览器中只要指定行(rows)和列(cols)属性,就可以规定 textarea的尺寸,大小就不会
- 简介subprocess是子流程,即进程的意思,该模块可以启动一个新进程,并连接到它们的输入/输出/错误管道,从而获取返回值。常用方法和接口
- 一、前言xlwt模块是python中专门用于写入Excel的拓展模块,可以实现创建表单、写入指定单元格、指定单元格样式等人工实现的功能,一句
- 本文实例讲述了Python实现批量将word转html并将html内容发布至网站的方法。分享给大家供大家参考。具体实现方法如下:#codin
- 自从腾讯微博上线以来,基本上就开始用了,一直到现在,作为一个开发人员,也看到了腾讯微博一直在不停的改变,也不知道大家有没有发现,腾讯微博提供
- 1、安装requests、xlrd、json、unittest库<1>pip 命令安装:pip install requests
- LBound函数:返回指定数组维的最小可用下标;UBound函数:返回指定数组维的最小可用上标;LBound(arrayname, [,di
- 前沿在CV领域,我们需要熟练掌握最基本的知识就是各种卷积神经网络CNN的模型架构,不管我们在图像分类或者分割,目标检测,NLP等,我们都会用
- keras非常方便。不解释,直接上实例。数据格式如下:序号 天气 是否周末 是否有促销 销量1 坏 &n
- 在windows下用python脚本实现文件的备份,参考《A Byte of Python3》第十一章(Page59)。#!/usr/bin
- 本文实例为大家分享了Django1.11自带分页器Django的具体使用方法,供大家参考,具体内容如下接下来我编写一个 views ,名cl
- 如何在网站上提供音乐下载?为用户提供歌曲下载,一般有两种方式,一是直接通过Http,浏览器下载,二是通过ftp协议下载。我们来用Http和浏
- 今天仔细研究了下GD的一些相关技术,顺手也研究下GD中文乱码的问题。 使用GD库输出中文字符串,调用imagestring是没有
- 详解Python中的三元运算什么是三元运算符在大部分编程语言中都有三目运算,也称三元运算,Python语言从Python2.5版本开始也引入
- Linux安装MySQL笔记1、在安装MySQL数据库服务器前,确保你的linux系统是可以连接网络的,下面我们将通过源码方式来安装mysq