参考:https://www.jb51.net/article/186659.htm

matplotlib plt.lengend图例放在图像的外侧

1、图例在图中实例


import numpy as np
import matplotlib.pyplot as plt# 定义x,y
X = np.linspace(0, 2*np.pi, 32, endpoint=True)
C = np.cos(X)# figure的名称
plt.figure('demon plot')
# 画图
plt.plot(X, C, 'r--', label = 'cos')# 显示x、y坐标代表的含义
plt.xlabel('Independent variable x')
plt.ylabel('dependent variable y')# 显示图的标题
plt.title(' demo')# 显示图例
# ------在图中右下角-------------
'''
loc的可选参数
0: 'best'
1: 'upper right'
2: 'upper left'
3: 'lower left'
4: 'lower right'
5: 'right'
6: 'center left'
7: 'center right'
8: 'lower center'
9: 'upper center'
10: 'center'
'''
plt.legend(loc='lower right')plt.show()

运行结果:

2、图例在图外

plt.legend(bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4)

说明:bbox_to_anchor被赋予的二元组中,num1用于控制legend的左右移动,值越大越向右边移动,num2用于控制legend的上下移动,值越大,越向上移动。用于微调图例的位置。

num1表示水平位置,num2表示垂直位置。

1)num1=0表示legend位于图像的左侧垂直线(其它设置:num2=0,num3=3,num4=0);

import numpy as np
import matplotlib.pyplot as plt# 定义x,y
X = np.linspace(0, 2*np.pi, 32, endpoint=True)
C = np.cos(X)# figure的名称
plt.figure('demon plot')
# 画图
plt.plot(X, C, 'r--', label = 'cos')# 显示x、y坐标代表的含义
plt.xlabel('Independent variable x')
plt.ylabel('dependent variable y')# 显示图的标题
plt.title(' demo')# 显示图例num1 = 0
num2 = 0
num3 = 3
num4 = 0
plt.legend(bbox_to_anchor=(num1, num2), loc=num3, borderaxespad=num4)plt.show()

运行结果:

2)num1=1表示legend位于图像的右侧垂直线(其它设置:num2=0,num3=3,num4=0);

运行结果:

3)为了美观,需要将legend放于图像的外侧,而又距离不是太大,一般设num1=1.01;

运行结果:

4)num2=0表示legend位于图像下侧水平线(其它设置num1=1.05,num3=3,num4=0);

运行结果:

5 )num2=1表示legend位于图像上侧水平线(其它设置:num1=1.05,num3=3,num4=0)。

运行结果:

matplotlib plt.lengend图例放在图像的外侧相关推荐

  1. matplotlib  plt.lengend

    参考文档   https://www.cnblogs.com/lfri/p/12248629.html 官方文档   https://matplotlib.org/api/_as_gen/matplo ...

  2. plt.legend 图例放在外面 子图会挤在一起 子图压缩 压扁

    因为设置了plt.tight_layout(),这会让子图挤在一起,需要先设置legend,再用plt.tight_layout() https://www.cnpython.com/qa/27701 ...

  3. python使用matplotlib可视化、为可视化图像添加图例(legend)、自定义图例的字体格式、字体大小、字体颜色等

    python使用matplotlib可视化.为可视化图像添加图例(legend).自定义图例的字体格式.字体大小.字体颜色等 目录

  4. Python的数据科学函数包(三)——matplotlib(plt)

    Matplotlib是Python最著名的2D绘图库 c opencv要比PIL, plt的速度更快一些 matplotlib中一张图的具体构造 如果将Matplotlib绘图和我们平常画画相类比,可 ...

  5. 为什么你用matplotlib savefig保存下来的图像是一个空白图片?

    为什么你用matplotlib savefig保存下来的图像是一个空白图片? plt.savefig( 'c.png') 亦或者: plt.figure() fig.savefig( 'plot.pn ...

  6. matplotlib之legend图例和标注(笔记三)

    代码: # -*- coding:utf-8 -*- #matplotlib设置legend图例 import matplotlib.pyplot as plt import numpy as npx ...

  7. 【Matplotlib】绘制多张图像时,上一张图像的内容重叠在下一张图像上

    问题描述 在使用matplotlib绘制多张图像时,可能出现两张图像的结果中出现重叠(画布重叠) import matplotlib.pyplot as pltx = [1, 2, 3, 4, 5] ...

  8. 处理opencv里用plt显示imread读取图像偏色问题

    处理opencv里用plt显示imread读取图像偏色问题 摘要 代码 看看结果: 原因: 解决方法: 看看结果 摘要 使用opecv过程中经常需要使用plt输出图片作为前后对比,方便观察. 但是会出 ...

  9. python使用matplotlib可视化、为可视化图像添加标题(title)、自定义标题的字体格式、字体大小、字体颜色等

    python使用matplotlib可视化.为可视化图像添加标题(title).自定义标题的字体格式.字体大小.字体颜色等 目录

最新文章

  1. linux ubuntu桌面进程,如何加快你的Ubuntu桌面性能
  2. android新年祝福代码,讯飞输入法发布Android新春版 Biu一下敲出美好祝愿!
  3. warning no newline at the end of file
  4. ui kit模板,让新手设计师临摹提高!
  5. 计算机体系结构_计算机体系结构知识笔记
  6. Lync Server 2013 安装准备工具 for Win 2008 R2
  7. python不能复制粘贴_你知道怎么使用python实现复制粘贴的功能吗?
  8. mfc最详细入门教程
  9. vc++中实现进度条
  10. 永恒之蓝漏洞复现(包括常见问题处理)和修复方案
  11. 使用json解决 《三级联动》(省市县) 含地区编号
  12. 苹果手机内存不够白屏
  13. 2020十大黄金配资交易平台排名
  14. SpringBoot 解析excel 使用easyexcel依赖 2.1.1版本
  15. GitHub 上受欢迎的 Android UI Library整理(part_two)
  16. 玩客云刷入openwrt系统
  17. AI-人工智能学习线路图
  18. win10鼠标右键恢复
  19. 1.Spring学习笔记_HelloWorld(by尚硅谷_佟刚)
  20. Python 定时发送【每日天气】和【每日简报】至【邮件】或【钉钉】(代码部署在云服务器)

热门文章

  1. 【asp.net小札记】Repeater自定义添加排序按钮+显示图片
  2. 你是我的四月天[转自天涯]
  3. Django进阶 Django REST framework+Vue 打造生鲜超市第一天 搭建环境
  4. 苹果一体机安装windows7黑屏解决方法
  5. 苹果台式一体机笔记本安装win双系统攻略教程
  6. linux下混杂模式
  7. [GWCTF 2019]枯燥的抽奖 1——php_mt_seed
  8. 关于spring的aop的xml和注解操作
  9. 文件上传漏洞全面渗透姿势总结
  10. 写个dump_stack【转】