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

智能推荐

C++ stringstream的用法-程序员宅基地

文章浏览阅读5.3k次,点赞2次,收藏6次。使用stringstream对象简化类型转换C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性、类型安全和可扩展性。在本文中,我将展示怎样使用这些库来实现安全和自动的类型转换。为什么要学习如果你已习惯了<stdio.h>风格的转换,也许你首先会问:为什么要花额外的精力来学习基于<sstream&g..._stringstream

Python爬虫-pyspider框架的使用_response.doc-程序员宅基地

文章浏览阅读1.5k次。pyspider 是一个用python实现的功能强大的网络爬虫系统,能在浏览器界面上进行脚本的编写,功能的调度和爬取结果的实时查看,后端使用常用的数据库进行爬取结果的存储,还能定时设置任务与任务优先级等。本篇文章只是对这个框架使用的大体介绍,更多详细信息可见官方文档。安装首先是环境的搭建,网上推荐的各种安装命令,如: pip install pyspider但是因为各种权限的..._response.doc

vue + blockly 自定义块、工具箱、主题_blockly vue-程序员宅基地

文章浏览阅读3.2k次。vue + blockly 自定义块、工具箱、主题自定义块建议使用 Blockly Developer Tools 方便而且选择多样,随时生成块代码。自定义块块由三个组件组成:块定义对象:定义块的外观和行为,包括文本、颜色、字段和连接。工具箱引用:对工具箱 XML 中块类型的引用,因此用户可以将其添加到工作区。生成器函数:生成此块的代码字符串。它总是用 JavaScript 编写,即使目标语言不是 JavaScript。通过 Blockly Developer Tools _blockly vue

笔记本显卡排名-程序员宅基地

文章浏览阅读158次。最新笔记本显卡性能排名总表最新笔记本显卡性能排名总表以下排名整理自国外网站,以性能排名,越往前性能越好第一梯队:高端显卡GeForce GTX 280M SLIMobility Radeon HD 4870 X2GeForce GTX 260M SLIGeForce 9800M GTX SLIGeForce GTX 280MGeForce 9800M G..._mobility radeon x700 win98

python中文显示不出来_解决Python词云库wordcloud不显示中文的问题-程序员宅基地

文章浏览阅读2.6k次。解决Python词云库wordcloud不显示中文的问题2018-11-25背景:wordcloud是基于Python开发的词云生成库,功能强大使用简单。github地址:https://github.com/amueller/word_cloudwordcloud默认是不支持显示中文的,中文会被显示成方框。安装:安装命令:pip install wordcloud解决:经过测试发现不支持显示中文..._词云python代码无法输出文字

随便推点

完成SSH项目 -- 实现dao层_ssh框架service层调用dao有的能创建成功-程序员宅基地

文章浏览阅读1.6k次。现在web02项目有了controller 和 service 但还没有dao层,接下来我们就整合dao层1:配置数据源 --- 使用c3p0数据源_ssh框架service层调用dao有的能创建成功

在.net下将word文档转换为加有水印pdf文档_.net webapi word pdf添加水印 开源-程序员宅基地

文章浏览阅读725次。前两天在.net平台下要做一个管理系统,其中要用到一个功能就是将word文档转换为加有水印的pdf文档。在网上找了不少代码,贴出来给大家分享一下。 所需软件:word2007 + 微软的SaveAsPDFandXPS.exe(下载地址为http://download.micros_.net webapi word pdf添加水印 开源

解决openweather无法注册的问题_openweather api 创建账户被禁止了-程序员宅基地

文章浏览阅读5.5k次。1.问题说明openweather注册不成功,无法进行机器人验证2.解决方法无法收到谷歌提供的机器人验证信息,科学上网可解决。科学上网用来注册,后续登录和获取数据不需要,直接访问即可。_openweather api 创建账户被禁止了

winscp通过跳板机访问远程服务器(使用秘钥的方式传输文件)_winscp 隧道 跳板机上的密码-程序员宅基地

文章浏览阅读4.7k次,点赞2次,收藏4次。一般需要ssh两个账户或两个设备才能有权限传输数据时经常遇到下面情况:我们一般连接跳板机使用的用户权限很小,能访问的文件不多,要是我想传输数据到我的设备上,却必须用跳板机连接设备怎么办?(别告诉我先在设备上搭个FTP服务器)下载winscp: 下载地址https://winscp.net/eng/downloads.php然后傻瓜式安装一键到底安装好就是配置访问服务器1、直接..._winscp 隧道 跳板机上的密码

从C++到Java(一)_enum c++ java-程序员宅基地

文章浏览阅读1.3k次。JAVA语言概括和基本类型,数组,枚举_enum c++ java

网络学习第六天(路由器、VLAN)_路由和vlan-程序员宅基地

文章浏览阅读316次。路由的概念路由器它称之为网关设备。路由器就是用于连接不同网络的设备路由器是位于OSI模型的第三层。路由器通过路由决定数据的转发。网关的背景:当时每家计算机厂商,用于交换数据的通信程序(协议)和数据描述格式各不相同。因此,就把用于相互转换这些协议和格式的计算机称为网关。路由器与三层交换器的对比路由协议对比路由器的作用:1.路由寻址2.实现不同网络之间相连的功能3.通过路由决定数据的转发,转发策略称为 路由选择。VLAN相关技术什么是VLAN?中文名称叫:虚拟局域网。虚_路由和vlan

推荐文章

热门文章

相关标签