matplotlib_legend

本文介绍matplotlib 中的 legend 图例,就是为了帮我们展示出每个数据对应的图像名称. 更好的让读者认识到你的数据结构.


Demo.py


import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-3, 3, 50)
y1 = 2*x + 1
y2 = x**2

plt.figure()
#set x limits
plt.xlim((-1, 2))
plt.ylim((-2, 3))

# set new sticks
new_sticks = np.linspace(-1, 2, 5)
plt.xticks(new_sticks)
# set tick labels
plt.yticks([-2, -1.8, -1, 1.22, 3],
           [r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
# set line syles
l1, = plt.plot(x, y2, label='linear line')
l2, = plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--', label='square line')
#plt.legend(loc='upper right')

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

结果:

Paste_Image.png

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 图例指南 原文:Legend guide 译者:飞龙 协议:CC BY-NC-SA 4.0 此图例指南是legen...
    布客飞龙阅读 2,727评论 1 5
  • Matplotlib 入门教程 来源:Introduction to Matplotlib and basic l...
    布客飞龙阅读 31,946评论 5 162
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,530评论 25 709
  • 十二寒岁,数不尽酸甜苦辣。 凿光映雪,韦编三绝榻。 明朝金榜,题字唯朱砂。 君看我,长安策马,踏尽香蹄花。 ...
    城子玄阅读 287评论 0 0
  • 我喜欢把行之有效的写作称之为“创作”,换言之,就是所写的文章能够顺利地通过简书的专题审批。顾名思义,创作就是运用我...
    摘星妮妮阅读 625评论 2 2