7-1 设置图例(纳入更多变量信息)

matplotlib中的图例设定

matplotlib.pyplot.legend(

handles : 需要设置图例标签的图形元素列表
labels : 相应图形元素的图例标签文字列表
loc = ‘upper right’ : 图例的显示位置,int/string
‘best’ 0
‘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
也可以按照2-tuple格式直接指定从左下角起的(x, y)坐标值
)
完整参考信息:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.legend.html
常见用法:

legend() # 显示图例
legend(labels) # 按默认元素顺序设定标签并显示图例
legend(handles, labels) # 设定指定元素的标签并显示图例

plt.plot([1,2,2,1], label='A line')

在这里插入图片描述

plt.plot([1,2,2,1], label='A line')
plt.legend()

在这里插入图片描述

plt.plot([1,2,2,1])
plt.legend(['Line Label'])

在这里插入图片描述

plt.plot([1,2,2,1])
plt.legend('Line Label') # 错误的设定方式

在这里插入图片描述

# 直接指定图例坐标
plt.plot([1,2,2,1])
plt.legend(['Line Label'], loc = (0.5,0.5)) 

在这里插入图片描述

# 此处赋值时需要以逗号结尾,否则返回的是对象列表
l1, = plt.plot([1, 2, 2, 1], label = 'sfsad') 
l2, = plt.plot([1, 1.5, 1.5, 1])
plt.legend(handles = [l1, l2], 
           labels = ['Line Label', 'line2'], loc = 'best')

在这里插入图片描述

# 不加逗号则需要准确指定到列表的具体对象
l1 = plt.plot([1,2,2,1])
l2 = plt.plot([1,1.5,1.5,1])
plt.legend(handles = [l1[0], l2[0]], 
           labels = ['Line Label', 'line2'], loc = 'best')

在这里插入图片描述

# 不加逗号则需要准确指定到列表的具体对象
l1 = plt.plot([1,2,2,1])
l2 = plt.plot([1,1.5,1.5,1], label = 'asdasdf')
# 只要求显示某些图形元素的标签
plt.legend(handles = [l1[0]], 
           labels = ['Line Label'], loc = 'best')

在这里插入图片描述

seaborn中的图例设定

图例也可以针对连续变量进行设定,此时可以修改图例的详细程度。

# 显示简化图例
sns.lineplot(x = 's4', y = 'index1', data = ccss, size = 'Qs9', ci = None)

在这里插入图片描述

# 完整显示详细图例
sns.lineplot(x = 's4', y = 'index1', data = ccss, size = 'Qs9', 
             legend = 'full', ci = None)

在这里插入图片描述

面板图形中的图例显示位置

# 面板图形中图例显示在外部
sns.lmplot(x = "s3", y="index1", hue = "s2", col = "s0", data = ccss)

在这里插入图片描述

# 面板图形中图例显示在内部
sns.lmplot(x = "s3", y="index1", hue = "s2", col = "s0", 
           data = ccss, legend_out = False)

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

juicy-hua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值