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

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

plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 'best')    或 直接loc = 0 :  plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 0)

除'best',另外loc属性有:'upper right', 'upper left', 'lower left', 'lower right', 'right', 'center left', 'center right', 'lower center', 'upper center', 'center'

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()

 

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值