相关文档:

控制图例入口

无参调用 legend() 会自动获取图例 handles 以及相关的 labels。其对应于以下代码:

handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, labels)

get_legend_handles_labels()方法返回 存在于图像中的 handles/artists 列表,这些图像可以用来生成结果图例中的入口。值得注意的是并不是所有的 artists 都可以被添加到图例中。

为了全部控制添加到图例中的内容,通常直接传递适量的 handles 给legend()函数。

line_up, = plt.plot([1,2,3], label='Line 2')
line_down, = plt.plot([3,2,1], label='Line 1')
plt.legend(handles=[line_up, line_down])

某些情况下,不太可能设置 handle 的 label,所以需要传递 labels 的列表给 legend()

line_up, = plt.plot([1,2,3], label='Line 2')
line_down, = plt.plot([3,2,1], label='Line 1')
plt.legend([line_up, line_down], ['Line Up', 'Line Down'])

综合例子如下:

import numpy as np
import matplotlib.pyplot as plt plt.figure(figsize=(8,5), dpi=80)
plt.subplot(111) X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C = np.cos(X)
S = np.sin(X) plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine") ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0)) plt.xlim(X.min() * 1.1, X.max() * 1.1)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$']) plt.ylim(C.min() * 1.1, C.max() * 1.1)
plt.yticks([-1, +1],
[r'$-1$', r'$+1$']) plt.legend(loc='upper left') plt.show()

图像表现形式如下:

最新文章

  1. InstallShield 脚本语言学习笔记
  2. C语言初级进阶1
  3. SQL Server2014,附加数据库失败,错误为:5120的解决方法
  4. 谷歌input框黄色背景问题
  5. hdu 4044 2011北京赛区网络赛E 树形dp ****
  6. FBI是如何破获“美国少女”裸照勒索案的
  7. PHP中file_put_contents追加时换行
  8. 【教程】如何正确的写一个Lemon/Cena的SPJ(special judge)
  9. POJ 1068 Parencodings 模拟 难度:0
  10. c#的基础入门回顾
  11. linux驱动分离分层的概念
  12. HTTP使用BASIC认证的原理及实现方法
  13. 解决TIME_WAIT过多问题
  14. Android开发之Toast解析
  15. HTTP压缩算法SDCH
  16. 看Lucene源码必须知道的基本概念
  17. python3 第四章 - 输入与输出
  18. Python format 格式化函数
  19. Python——Django-urls.py的作用
  20. Django模型操作常用方法

热门文章

  1. [cb]ScriptableObject 序列化
  2. Jenkins遇到问题二:Jenkins服务器磁盘空间管理策略
  3. jenkins配置记录(1)--添加用户权限
  4. getEl mask 用法
  5. STL整理
  6. sp_executesql介绍和使用
  7. Git之分支创建策略
  8. Elasticsearch集群中处理大型日志流的几个常用概念
  9. c语言 &取地址运算符的理解
  10. [CareerCup] 10.3 Integer not Contain in the File 文件中不包含的数