Py之matplotlib.pyplot:matplotlib.pyplot的plt.legend函数的简介、使用方法之详细攻略


纯真用发夹
纯真用发夹 2022-09-19 11:41:23 51658
分类专栏: 资讯

Py之matplotlib.pyplot:matplotlib.pyplot的plt.legend函数的简介、使用方法之详细攻略

目录

matplotlib.pyplot的plt.legend函数的简介

1、参数解释

2、源代码

matplotlib.pyplot的plt.legend函数的使用方法

1、基础用法


matplotlib.pyplot的plt.legend函数的简介

          legend模块定义了legend类,负责绘制与轴和/或图形相关的图例。Legend类是一个图例句柄和图例文本的容器,该函数是用来给当前图像添加图例内容。大多数用户通常会通过图例函数创建图例。图例处理程序映射指定如何在轴或图形中从artists(线、补丁等)创建图例句柄。默认的图例处理程序定义在legend_handler模块中。虽然默认的图例处理程序并没有覆盖所有的artists类型,但是可以定义自定义图例处理程序来支持任意对象。

  1. plt.legend(loc='best',frameon=False) frameon参数,去掉图例边框
  2. plt.legend(loc='best',edgecolor='blue') edgecolor参数,设置图例边框颜色
  3. plt.legend(loc='best',facecolor='blue') facecolor参数,设置图例背景颜色,若无边框,参数无效
  4. plt.legend(["CH", "US"], title='China VS Us') 设置图例标题
  5. plt.legend([p1, p2], ["CH", "US"]) 设置图例名字及对应关系

原始文档https://matplotlib.org/api/legend_api.html?highlight=legendmodule-matplotlib.legend

1、参数解释

参数解释具体应用
loc

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

plt.legend(loc='upper center')
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'
propthe font property字体参数 
fontsizethe font size (used only if prop is not specified)

fontsize : int or float or {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’}

markerscalethe relative size of legend markers vs. original

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

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

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

 
numpointsthe number of points in the legend for line

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

 
scatterpointsthe number of points in the legend for scatter plot

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

 
scatteryoffsetsa list of yoffsets for scatter symbols in legend

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

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

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

 
fancyboxIf True, draw the frame with a round fancybox.

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

 
shadowIf True, draw a shadow behind legend.

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

 
framealphaTransparency of the frame.

控制图例框架的 Alpha 透明度

 
edgecolorFrame edgecolor. 
facecolorFrame facecolor. 
ncolnumber of columns 设置图例分为n列展示 
borderpadthe fractional whitespace inside the legend border

图例边框的内边距

 
labelspacingthe vertical space between the legend entries

图例条目之间的垂直间距

 
handlelengththe length of the legend handles 

图例句柄的长度

 
handleheightthe height of the legend handles 

图例句柄的高度

 
handletextpadthe pad between the legend handle and text 

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

 
borderaxespadthe pad between the axes and legend border

轴与图例边框之间的距离

 
columnspacingthe spacing between columns 列间距 
titlethe legend title 
bbox_to_anchorthe bbox that the legend will be anchored.指定图例在轴的位置 
bbox_transformthe transform for the bbox. transAxes if None. 

2、源代码

更新……

  1. def legend Found at: matplotlib.pyplot
  2. -meta">@_copy_docstring_and_deprecators(Axes.legend)
  3. def legend(*args, **kwargs):
  4. return gca().legend(*args, **kwargs)
  5. Autogenerated by boilerplate.py. Do not edit as changes will be lost.


matplotlib.pyplot的plt.legend函数的使用方法

1、基础用法

  1. plt.figure()
  2. col_cou_len=len(Keys)
  3. plt.pie(x=Values,labels=Keys,colors=cols[:col_cou_len], startangle=90,shadow=True,autopct='%1.3f%%')
  4. plt.title(tit_name)
  5. plt.legend()
  6. plt.show()
文章知识点与官方知识档案匹配,可进一步学习相关知识

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=2128
赞同 0
评论 0 条
纯真用发夹L0
粉丝 0 发表 13 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2959
【软件正版化】软件正版化工作要点  2878
统信UOS试玩黑神话:悟空  2843
信刻光盘安全隔离与信息交换系统  2737
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1270
grub引导程序无法找到指定设备和分区  1235
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  165
点击报名 | 京东2025校招进校行程预告  164
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  163
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  159
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!