python - matplotlib.legend()函数用法解析_python legend函数-程序员宅基地

技术标签: python  

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

 

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

Keyword

Description

loc

Location code string, or tuple (see below).图例所有figure位置

prop

the font property字体参数

fontsize

the font size (used only if prop is not specified)

markerscale

the relative size of legend markers vs. original

图例标记与原始标记的相对大小

markerfirst

If True (default), marker is to left of the label.

如果为True,则图例标记位于图例标签的左侧

numpoints

the number of points in the legend for line

为线条图图例条目创建的标记点数

scatterpoints

the number of points in the legend for scatter plot

为散点图图例条目创建的标记点数

scatteryoffsets

a list of yoffsets for scatter symbols in legend

为散点图图例条目创建的标记的垂直偏移量

frameon

If True, draw the legend on a patch (frame).

控制是否应在图例周围绘制框架

fancybox

If True, draw the frame with a round fancybox.

控制是否应在构成图例背景的FancyBboxPatch周围启用圆边

shadow

If True, draw a shadow behind legend.

控制是否在图例后面画一个阴影

framealpha

Transparency of the frame.

控制图例框架的 Alpha 透明度

edgecolor

Frame edgecolor.

facecolor

Frame facecolor.

ncol

number of columns 设置图例分为n列展示

borderpad

the fractional whitespace inside the legend border

图例边框的内边距

labelspacing

the vertical space between the legend entries

图例条目之间的垂直间距

handlelength

the length of the legend handles 

图例句柄的长度

handleheight

the height of the legend handles 

图例句柄的高度

handletextpad

the pad between the legend handle and text 

图例句柄和文本之间的间距

borderaxespad

the pad between the axes and legend border

轴与图例边框之间的距离

columnspacing

the spacing between columns 列间距

title

the legend title

bbox_to_anchor

the bbox that the legend will be anchored.指定图例在轴的位置

bbox_transform

the transform for the bbox. transAxes if None.

 

 

 

(1)设置图例位置

使用loc参数

plt.legend(loc='lower left')

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'

 

(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到图形边界外

 
  1. #主要是bbox_to_anchor的使用

  2. box = ax1.get_position()

  3. ax1.set_position([box.x0, box.y0, box.width , box.height* 0.8])

  4. ax1.legend(loc='center', bbox_to_anchor=(0.5, 1.2),ncol=3)

 

 

4.案例:显示多图例legend

 
  1. import matplotlib.pyplot as plt

  2. import numpy as np

  3. x = np.random.uniform(-1, 1, 4)

  4. y = np.random.uniform(-1, 1, 4)

  5. p1, = plt.plot([1,2,3])

  6. p2, = plt.plot([3,2,1])

  7. l1 = plt.legend([p2, p1], ["line 2", "line 1"], loc='upper left')

  8.  
  9. p3 = plt.scatter(x[0:2], y[0:2], marker = 'D', color='r')

  10. p4 = plt.scatter(x[2:], y[2:], marker = 'D', color='g')

  11. # This removes l1 from the axes.

  12. plt.legend([p3, p4], ['label', 'label1'], loc='lower right', scatterpoints=1)

  13. # Add l1 as a separate artist to the axes

  14. plt.gca().add_artist(l1)

 

 
  1. import matplotlib.pyplot as plt

  2. line1, = plt.plot([1,2,3], label="Line 1", linestyle='--')

  3. line2, = plt.plot([3,2,1], label="Line 2", linewidth=4)

  4. # 为第一个线条创建图例

  5. first_legend = plt.legend(handles=[line1], loc=1)

  6. # 手动将图例添加到当前轴域

  7. ax = plt.gca().add_artist(first_legend)

  8. # 为第二个线条创建另一个图例

  9. plt.legend(handles=[line2], loc=4)

  10. plt.show()

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

智能推荐

spring Boot 中使用线程池异步执行多个定时任务_springboot启动后自动开启多个线程程序-程序员宅基地

文章浏览阅读4.1k次,点赞2次,收藏6次。spring Boot 中使用线程池异步执行多个定时任务在启动类中添加注解@EnableScheduling配置自定义线程池在启动类中添加注解@EnableScheduling第一步添加注解,这样才会使定时任务启动配置自定义线程池@Configurationpublic class ScheduleConfiguration implements SchedulingConfigurer..._springboot启动后自动开启多个线程程序

Maven编译打包项目 mvn clean install报错ERROR_mvn clean install有errors-程序员宅基地

文章浏览阅读1.1k次。在项目的target文件夹下把之前"mvn clean package"生成的压缩包(我的是jar包)删掉重新执行"mvn clean package"再执行"mvn clean install"即可_mvn clean install有errors

navacate连接不上mysql_navicat连接mysql失败怎么办-程序员宅基地

文章浏览阅读974次。Navicat连接mysql数据库时,不断报1405错误,下面是针对这个的解决办法:MySQL服务器正在运行,停止它。如果是作为Windows服务运行的服务器,进入计算机管理--->服务和应用程序------>服务。如果服务器不是作为服务而运行的,可能需要使用任务管理器来强制停止它。创建1个文本文件(此处命名为mysql-init.txt),并将下述命令置于单一行中:SET PASSW..._nvarchar链接不上数据库

Python的requests参数及方法_python requests 参数-程序员宅基地

文章浏览阅读2.2k次。Python的requests模块是一个常用的HTTP库,用于发送HTTP请求和处理响应。_python requests 参数

近5年典型的的APT攻击事件_2010谷歌网络被极光黑客攻击-程序员宅基地

文章浏览阅读2.7w次,点赞7次,收藏50次。APT攻击APT攻击是近几年来出现的一种高级攻击,具有难检测、持续时间长和攻击目标明确等特征。本文中,整理了近年来比较典型的几个APT攻击,并其攻击过程做了分析(为了加深自己对APT攻击的理解和学习)Google极光攻击2010年的Google Aurora(极光)攻击是一个十分著名的APT攻击。Google的一名雇员点击即时消息中的一条恶意链接,引发了一系列事件导致这个搜_2010谷歌网络被极光黑客攻击

Android 开发的现状及发展前景_android现状-程序员宅基地

文章浏览阅读8.8k次,点赞3次,收藏31次。在几年前的时候,曾听过很多人说 Android 学习很简单,做个App就上手了,工作机会多,毕业后也比较容易找工作。这种观点可能是很多Android开发者最开始入行的原因之一。在工作初期,工作主要是按照业务需求实现App页面的功能,按照设计师的设计稿实现页面的效果。在实现的过程中,总是会被提如下的需求:这个字能不能大点或者醒目点儿?感觉颜色和设计稿有差别,能不能再调调?怎么老是崩溃啊,行不行啊?…所以,工作过一、两年后你会发现,自己每天重复工作内容就是将找各种各样的组件、框架,拖拖拽拽,改_android现状

随便推点

微信小程序api视频课程-定时器-setTimeout的使用_微信小程序 settimeout 向上层传值-程序员宅基地

文章浏览阅读1.1k次。JS代码 /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { setTimeout( function(){ wx.showToast({ title: '黄菊华老师', }) },2000 ) },说明该代码只执行一次..._微信小程序 settimeout 向上层传值

uploadify2.1.4如何能使按钮显示中文-程序员宅基地

文章浏览阅读48次。uploadify2.1.4如何能使按钮显示中文博客分类:uploadify网上关于这段话的搜索恐怕是太多了。方法多也试过了不知怎么,反正不行。最终自己想办法给解决了。当然首先还是要有fla源码。直接去管网就可以下载。[url]http://www.uploadify.com/wp-content/uploads/uploadify-v2.1.4...

戴尔服务器安装VMware ESXI6.7.0教程(U盘安装)_vmware-vcsa-all-6.7.0-8169922.iso-程序员宅基地

文章浏览阅读9.6k次,点赞5次,收藏36次。戴尔服务器安装VMware ESXI6.7.0教程(U盘安装)一、前期准备1、下载镜像下载esxi6.7镜像:VMware-VMvisor-Installer-6.7.0-8169922.x86_64.iso这里推荐到戴尔官网下载,Baidu搜索“戴尔驱动下载”,选择进入官网,根据提示输入服务器型号搜索适用于该型号服务器的所有驱动下一步选择具体类型的驱动选择一项下载即可待下载完成后打开软碟通(UItraISO),在“文件”选项中打开刚才下载好的镜像文件然后选择启动_vmware-vcsa-all-6.7.0-8169922.iso

百度语音技术永久免费的语音自动转字幕介绍 -程序员宅基地

文章浏览阅读2k次。百度语音技术永久免费的语音自动转字幕介绍基于百度语音技术,识别率97%无时长限制,无文件大小限制永久免费,简单,易用,速度快支持中文,英文,粤语永久免费的语音转字幕网站: http://thinktothings.com视频介绍 https://www.bilibili.com/video/av42750807 ...

Dyninst学习笔记-程序员宅基地

文章浏览阅读7.6k次,点赞2次,收藏9次。Instrumentation是一种直接修改程序二进制文件的方法。其可以用于程序的调试,优化,安全等等。对这个词一般的翻译是“插桩”,但这更多使用于软件测试领域。【找一些相关的例子】Dyninst可以动态或静态的修改程序的二进制代码。动态修改是在目标进程运行时插入代码(dynamic binary instrumentation)。静态修改则是直接向二进制文件插入代码(static b_dyninst

在服务器上部署asp网站,部署asp网站到云服务器-程序员宅基地

文章浏览阅读2.9k次。部署asp网站到云服务器 内容精选换一换通常情况下,需要结合客户的实际业务环境和具体需求进行业务改造评估,建议您进行服务咨询。这里仅描述一些通用的策略供您参考,主要分如下几方面进行考虑:业务迁移不管您的业务是否已经上线华为云,业务迁移的策略是一致的。建议您将时延敏感型,有快速批量就近部署需求的业务迁移至IEC;保留数据量大,且需要长期稳定运行的业务在中心云上。迁移方法请参见如何计算隔离独享计算资源..._nas asp网站