matplotlib中的legend()——用于显示图例

legend()的一个用法:

当我们有多个 axes时,我们如何把它们的图例放在一起呢??

我们可以这么做:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(1, 11)

fig = plt.figure(1)
ax1 = plt.subplot(2, 1, 1)
ax2 = plt.subplot(2, 1, 2)
l1, = ax1.plot(x, x*x, 'r')             #这里关键哦
l2, = ax2.plot(x, x*x, 'b')           # 注意

plt.legend([l1, l2], ['first', 'second'], loc = 'upper right')             #其中,loc表示位置的;

plt.show()

 

在legend的参数中, loc参数设置图例的显示位置的:

'best'         : 0, (only implemented for axes legends)(自适应方式)
'upper right'  : 1,
'upper left'   : 2,
'lower left'   : 3,
'lower right'  : 4,
'right'        : 5,
'center left'  : 6,
'center right' : 7,
'lower center' : 8,
'upper center' : 9,
'center'       : 10,

 

另外,还有控制位置的重要参数:bbox_to_anchor(num1, num2),  bbox_to_anchor被赋予的二元组中,第一个数值用于控制legend的左右移动,值越大越向右边移动,第二个数值用于控制legend的上下移动,值越大,越向上移动。

其它参数看这里有:

Keyword
Description
loc
a location code
prop
the font property (matplotlib.font_manager.FontProperties 对象)
eg
song_font = matplotlib.font_manager.FontProperties(fname='simsun.ttc', size=8)
fontsize
the font size (和prop互斥,不可同时使用)
markerscale
the relative size of legend markers vs. original
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 a frame around the legend. If None, use rc
fancybox
if True, draw a frame with a round fancybox. If None, use rc
shadow
if True, draw a shadow behind legend
ncol
number of columns
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 length 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
matplotlib图例边框是指在绘制图例时,图例框的边界线。可以通过设置不同的参数来控制图例边框的外观。 在matplotlib,可以使用legend函数来添加图例,并通过参数设置图例的边框。其,常用的参数包括frameon、edgecolor和linewidth等。 - frameon参数用于控制是否显示图例的边框,其值为True表示显示,False表示不显示。默认值为True。 - edgecolor参数用于设置图例边框的颜色,可以接受各种颜色的字符串或RGBA元组,默认值为'black'。 - linewidth参数用于设置图例边框的线宽,其值为一个浮点数,默认值为1.0。 以下是一个例子: ```python import matplotlib.pyplot as plt # 绘制数据曲线 x = [1, 2, 3] y = [4, 5, 6] plt.plot(x, y, label='数据曲线') # 添加图例并设置边框 legend = plt.legend(frameon=True, edgecolor='red', linewidth=2) # 显示图形 plt.show() ``` 在这个例子legend函数被用来添加图例,并通过frameon参数设置为显示图例边框,edgecolor参数设置为红色边框线,linewidth参数设置为2.0的线宽。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【python学习】-matplotlib图形设置(线宽、标签、颜色、图框、线类型、图例大小位置、图框大小及像素等)](https://blog.csdn.net/qq_40481843/article/details/106231257)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Matplotlib(6)——图例设置](https://blog.csdn.net/weixin_44830542/article/details/111114684)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值