1.图例legend基础语法及用法

legend语法参数如下: matplotlib.pyplot.legend(*args, **kwargs)



(1)设置图例位置
使用loc参数
plt.legend(loc=‘lower left’)

(2)设置图例字体
#设置字体大小
fontsize : int or float or {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’}

(3)设置图例边框及背景
plt.legend(loc=‘best’,frameon=False) #去掉图例边框
plt.legend(loc=‘best’,edgecolor=‘blue’) #设置图例边框颜色
plt.legend(loc=‘best’,facecolor=‘blue’) #设置图例背景颜色,若无边框,参数无效

(4)设置图例标题
plt.legend(loc=‘best’,title=‘figure 1 legend’) #去掉图例边框

2.legend面向对象命令

(1)获取并设置legend图例
plt.legend(loc=0, numpoints=1)
leg = plt.gca().get_legend() #或leg=ax.get_legend()
ltext = leg.get_texts()
plt.setp(ltext, fontsize=12,fontweight=‘bold’)

(2)设置图例
legend = ax.legend((rectsTest1, rectsTest2, rectsTest3), (‘test1’, ‘test2’, ‘test3’))
legend = ax.legend(loc=‘upper center’, shadow=True, fontsize=‘x-large’)
legend.get_frame().set_facecolor(‘red’) #设置图例legend背景为红色
frame = legend.get_frame()
frame.set_alpha(1)
frame.set_facecolor(‘none’) #设置图例legend背景透明

(3)移除图例
ax1.legend_.remove() ##移除子图ax1中的图例
ax2.legend_.remove() ##移除子图ax2中的图例
ax3.legend_.remove() ##移除子图ax3中的图例

3.案例:设置图例legend到图形边界外

#主要是bbox_to_anchor的使用
box = ax1.get_position()
ax1.set_position([box.x0, box.y0, box.width , box.height* 0.8])
ax1.legend(loc='center', bbox_to_anchor=(0.5, 1.2),ncol=3)

4.案例:显示多图例legend

import matplotlib.pyplot as plt
import numpy as np
x = np.random.uniform(-1, 1, 4)
y = np.random.uniform(-1, 1, 4)
p1, = plt.plot([1,2,3])
p2, = plt.plot([3,2,1])
l1 = plt.legend([p2, p1], ["line 2", "line 1"], loc='upper left')p3 = plt.scatter(x[0:2], y[0:2], marker = 'D', color='r')
p4 = plt.scatter(x[2:], y[2:], marker = 'D', color='g')
# This removes l1 from the axes.
plt.legend([p3, p4], ['label', 'label1'], loc='lower right', scatterpoints=1)
# Add l1 as a separate artist to the axes
plt.gca().add_artist(l1)

import matplotlib.pyplot as plt
line1, = plt.plot([1,2,3], label="Line 1", linestyle='--')
line2, = plt.plot([3,2,1], label="Line 2", linewidth=4)
# 为第一个线条创建图例
first_legend = plt.legend(handles=[line1], loc=1)
# 手动将图例添加到当前轴域
ax = plt.gca().add_artist(first_legend)
# 为第二个线条创建另一个图例
plt.legend(handles=[line2], loc=4)
plt.show()

matplotlib命令与格式:图例legend语法及设置相关推荐

  1. matplotlib命令与格式:图像(figure)与子区域(axes)布局与规划

    1.figure语法及操作 (1)figure语法说明 figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, ...

  2. matplotlib命令与格式:标题(title),标注(annotate),文字说明(text)-------(含绘图实例演示)

    Python 全栈工程师核心面试 300 问深入解析(2020 版)----全文预览 Python 全栈工程师核心面试 300 问深入解析(2020 版)----欢迎订阅 1.title设置图像标题 ...

  3. matplotlib命令与格式:标题(title),标注(annotate),文字说明(text)

    1.title设置图像标题 (1)title常用参数 fontsize设置字体大小,默认12,可选参数 ['xx-small', 'x-small', 'small', 'medium', 'larg ...

  4. 原 matplotlib命令与格式:系统字体与显示中文

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/helunqu2017/article/ ...

  5. matplotlib命令与格式:标题(title)

    1.title设置图像标题 (1)title常用参数 fontsize设置字体大小,默认12,可选参数 ['xx-small', 'x-small', 'small', 'medium', 'larg ...

  6. Python可视化(matplotlib)图像自定义图例(Legend)

    Python可视化(matplotlib)图像自定义图例(Legend) 目录 Python可视化(matplotlib)图像自定义图例(Legend) 简单图例

  7. python中matplotlib库legend_matplotlib中的legend()—显示图例

    legend()的一个用法: 当我们有多个 axes时,我们如何把它们的图例放在一起呢?? 我们可以这么做: import numpy as np x = np.arange(1, 11) fig = ...

  8. Python Matplotlib数据可视化绘图之(六)————图片大小、颜色、标题、纵横坐标、画布和绘图区域背景颜色、Legend(图例)等的参数设置详解

    文章目录 前言 一.图片大小的设定和调节 1. 三种调整matplotlib图片大小的方法 1.1 plt.figure(figsize=(n, n)) 1.1.1 代码如下: 1.1.2 输出结果如 ...

  9. matlab 添加子图图案,matplotlib给子图添加图例的方法

    matplotlib给子图添加图例的方法 代码如下: import matplotlib.pyplot as plt x = [1,2,3,4,5,6,7,8] y = [5,2,4,2,1,4,5, ...

最新文章

  1. [转载翻译][重新整理]西川善司的”METAL GEAR SOLID 4”图形讲座(5)
  2. DataList编辑、更新、取消、删除、分页(分页控件 AspNetPager.dll)
  3. how to become a very good candidate at the investment bank?
  4. python numpy 子数组_Python快速转换numpy数组中Nan和Inf的方法实例说明
  5. 关于项目管理的一些想法
  6. python程序显示自己的版权_手把手教你Pycharm皮肤主题及个性化设置,python程序员必备-Go语言中文社区...
  7. 计算机网络的带宽是指网络可通过的,计算机网络及带宽概念.ppt
  8. OGRE源代码resource分析
  9. Python科学计算
  10. 泰勒·斯威夫特(Taylor Swift)最好听歌曲专辑,喜欢的可以下载保存
  11. 【支付宝】支付宝ISV申请方法
  12. python爬取百度图片的思路与代码(最后附上了代码)
  13. 区块链对人工智能的变革:去中心化将带来数据新范式
  14. C# 批量图片打包下载
  15. 20届icoding 实验1
  16. 判断当前音效是否播放完毕
  17. Win11投屏时禁止通知的方法
  18. 【Web项目】点餐系统
  19. RT-ThreadXSTM32F407智能车培训报名啦!
  20. 哈拉比数据库;为了家人

热门文章

  1. JavaMail实现邮箱激活功能
  2. bfv同态加密_微软同态加密库SEAL使用
  3. unturned 服务器修改,unturned 服务器设置
  4. 修复好一个科脉软件-科脉御商软件数据库损坏问题
  5. ChromeDriver资源下载
  6. python识别验证码之分离图片验证码颜色通道
  7. 利用5次shift弹出粘滞键破解部分win7/win10密码
  8. iWatch 开发 1: 你想知道的 iWatch App 开发
  9. SQL server笔记01(数据查询前内容)
  10. 渗透测试用工具(三)layer子域名挖掘机