在使用matplotlib画图时,少不了对性能图形做出一些说明和补充。一般情况下,loc属性设置为'best'就足够应付了

plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 'best')

或直接loc = 0

plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 0)

关于matplotlib-legend 位置属性 loc 使用说明

除'best',另外loc属性有:

'upper right', 'upper left', 'lower left', 'lower right', 'right', 'center left', 'center right', 'lower center', 'upper center', 'center'

关于matplotlib-legend 位置属性 loc 使用说明

不说太多,上面是全部的快捷使用,满足一般需求。

demo:

import matplotlib.pyplot as plt
import numpy as np
 
# 绘制普通图像
x = np.linspace(-1, 1, 50)
y1 = 2 * x + 1
y2 = x**2
 
plt.figure()
# 在绘制时设置lable, 逗号是必须的
l1, = plt.plot(x, y1, label = 'line')
l2, = plt.plot(x, y2, label = 'parabola', color = 'red', linewidth = 1.0, linestyle = '--')
 
# 设置坐标轴的取值范围
plt.xlim((-1, 1))
plt.ylim((0, 2))
 
# 设置坐标轴的lable
plt.xlabel('X axis')
plt.ylabel('Y axis')
 
# 设置x坐标轴刻度, 原来为0.25, 修改后为0.5
plt.xticks(np.linspace(-1, 1, 5))
# 设置y坐标轴刻度及标签, $$是设置字体
plt.yticks([0, 0.5], ['$minimum$', 'normal'])
 
# 设置legend
plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 'best')
plt.show()

运行结果:

关于matplotlib-legend 位置属性 loc 使用说明

补充知识:设置图列(key/legend)的位置和大小 --gnuplot

先看几个例子:

//不显示图例。
unset key
//设置图例 显示在图形(内)的顶部居中,并且多个图例水平显示。
set key top horizontal center
//设置图例 显示在图形(外)的顶部居中,并且多个图例水平显示。
set key top outside horizontal center
//设置图例 显示的字体并加粗。
set key font "Times,18,Bold"
//调整图例行间隔
set key spacing 3
//调整图例中线段示例长度
set key samplen 2

set key 的语法规则

Syntax: 
   set key {on|off} {default}
       {{inside | outside} | {lmargin | rmargin | tmargin | bmargin}
        | {at <position>}}
       {left | right | center} {top | bottom | center}
       {vertical | horizontal} {Left | Right}
       {{no}reverse} {{no}invert}
       {samplen <sample_length>} {spacing <vertical_spacing>}
       {width <width_increment>}
       {height <height_increment>}
       {{no}autotitle {columnheader}}
       {title "<text>"} {{no}enhanced}
       {{no}box { {linestyle | ls <line_style>}
            | {linetype | lt <line_type>}
             {linewidth | lw <line_width>}}}
   unset key
   show key

Elements within the key are stacked according to vertical or horizontal. In the case of vertical, the key occupies as few columns as possible. That is, elements are aligned in a column until running out of vertical space at which point a new column is started. In the case of horizontal, the key occupies as few rows as possible.

图例是依据我们设置的水平显示或垂直显示进行堆叠式地显示。

对于垂直显示,pnuplot会占用尽可能少的行来放置我们的图例,当图例在一行显示不下时,它会另启一行来显示。

对于水平显示方式,pnuplot会占用尽可能少的列来放置我们的图例,当图例在一列显示不下时,它会另启一列来放置。

The vertical spacing between lines is controlled by spacing. The spacing is set equal to the product of the pointsize, the vertical tic size, and vertical_spacing. The program will guarantee that the vertical spacing is no smaller than the character height.

The defaults for set key are on, right, top, vertical, Right, noreverse, noinvert, samplen 4, spacing 1.25, title “”, and nobox.

以上这篇关于matplotlib-legend 位置属性 loc 使用说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
matplotlib,legend,loc

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。