源自  matplotlib中的legend()——用于显示图例 -- 博客园 http://www.cnblogs.com/yinheyi/p/6792120.html

legend()的一个用法:

当我们有多个 axes时,我们如何把它们的图例放在一起呢??

我们可以这么做:

import numpy as np

x = np.arange(1, 11)

fig = plt.figure(1)
ax1 = plt.subplot(2, 1, 1)
ax2 = plt.subplot(2, 1, 2)
l1, = ax1.plot(x, x*x, 'r') #这里关键哦
l2, = ax2.plot(x, x*x, 'b') # 注意 plt.legend([l1, l2], ['first', 'second'], loc = 'upper right') #其中,loc表示位置的; plt.show()

在legend的参数中, loc参数设置图例的显示位置的:

'best'         : 0, (only implemented for axes legends)(自适应方式)
'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,

另外,还有控制位置的重要参数:bbox_to_anchor(num1, num2),  bbox_to_anchor被赋予的二元组中,第一个数值用于控制legend的左右移动,值越大越向右边移动,第二个数值用于控制legend的上下移动,值越大,越向上移动。

其它参数看这里有:

Keyword
Description
loc
a location code
prop
the font property (matplotlib.font_manager.FontProperties 对象)
eg
song_font = matplotlib.font_manager.FontProperties(fname='simsun.ttc', size=8)
fontsize
the font size (和prop互斥,不可同时使用)
markerscale
the relative size of legend markers vs. original
numpoints
the number of points in the legend for line
scatterpoints
the number of points in the legend for scatter plot
scatteryoffsets
a list of yoffsets for scatter symbols in legend
frameon
if True, draw a frame around the legend. If None, use rc
fancybox
if True, draw a frame with a round fancybox. If None, use rc
shadow
if True, draw a shadow behind legend
ncol
number of columns
borderpad
the fractional whitespace inside the legend border
labelspacing
the vertical space between the legend entries
handlelength
the length of the legend handles
handleheight
the length of the legend handles
handletextpad
the pad between the legend handle and text
borderaxespad
the pad between the axes and legend border
columnspacing
the spacing between columns
title
the legend title
bbox_to_anchor
the bbox that the legend will be anchored.
bbox_transform
the transform for the bbox. transAxes if None.

matplotlib中的legend()—显示图例的更多相关文章

  1. Matplotlib中中文不显示问题

    我们在使用jupter进行数据分析的时候,会接触到Matplotlib这个库,它是用来进行可视化数据分析的,在一个图中,我们常常会加入一些中文来进行说明.当我们加入中文的时候会出现下图所示的样子: 可 ...

  2. python+NLTK 自然语言学习处理三:如何在nltk/matplotlib中的图片中显示中文

    我们首先来加载我们自己的文本文件,并统计出排名前20的字符频率 if __name__=="__main__": corpus_root='/home/zhf/word' word ...

  3. matplotlib中的legend()——用于显示图例

    legend()的一个用法: 当我们有多个 axes时,我们如何把它们的图例放在一起呢?? 我们可以这么做: import matplotlib.pyplot as plt import numpy ...

  4. Python Matplotlib 中对于 bar 显示时间的问题

    参考: 官方教程: http://matplotlib.org/1.3.1/users/recipes.html http://stackoverflow.com/questions/13515471 ...

  5. matplotlib画图无法显示图例 报错No handles with labels found to put in legend.

    很久没有matplotlib了,今天画图的时候发现了一个很小的问题....明明加了legend(),图表会出来,却无法正常显示图例.最后发现只要在plt.plot()加label图例就可以正常显示了.

  6. Matplotlib 中常见的图形

    # 导包 from matplotlib import pyplot as plt import numpy as np 线性图 简单线性图 在图表的所有类型中,线性图最为简单.线性图的各个数据点由一 ...

  7. matplotlib中subplots的用法

    1.matplotlib中如果只画一张图的话,可以直接用pyplot,一般的做法是: import matplotlib.pyplot as plt plt.figure(figsize=(20,8) ...

  8. matplotlib y轴标注显示不全以及subplot调整的问题

    matplotlib y轴标注显示不全以及subplot调整的问题 问题: 我想在y轴显示的标注太长,想把它变成两行显示,发现生成的图形只显示的第二行的字,把第一行的字挤出去了 想要的是显示两行这样子 ...

  9. matplotlib 中的一些参数设置

    首先:在pycharm 中要使图显示出来,最后一定要加上 plt.show(),如: plt.bar(x, y) plt.show() 下面就是我使用 matplotlib  遇到的一些常用参数设置: ...

随机推荐

  1. web页面防盗链功能使用--request.getHeader("Referer")

    使用Request对象设置页面的防盗链 所谓的防盗链就是当你以一个非正常渠道去访问某一个Web资源的时候,服务器会将你的请求忽略并且将你的当前请求变为按正常渠道访问时的请求并返回到相应的页面,用户只有 ...

  2. linux文件目录连接

    linux系统下提供ln指令来进行文件链接.文件链接主要分为硬链接和软链接. 硬链接:由于linux下的文件是通过索引节点(Inode)来识别文件,硬链接可以认为是一个指针,指向文件索引节点的指针,系 ...

  3. 【POJ】1094 Sorting It All Out(拓扑排序)

    http://poj.org/problem?id=1094 原来拓扑序可以这样做,原来一直sb的用白书上说的dfs............ 拓扑序只要每次将入度为0的点加入栈,然后每次拓展维护入度即 ...

  4. HMLocationEvent

    HMLocationEvent *locEvent = [[HMLocationEvent alloc] initWithRegion:region1]; region1.notifyOnEntry ...

  5. hdu 1174:爆头(计算几何,三维叉积求点到线的距离)

    爆头 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  6. Linux系统下MySQL数据库的备份和恢复

    当我们MySQL数据库保存重要数据的时候,备份工作极为重要.本文介绍如何使用mysqldump备份和恢复数据,使用该方法,可以将数据库中的数据备份成一个文本文件,也可将备份好的数据库迁移到另一台的服务 ...

  7. AWS系列-EC2实例选择镜像

    Centos Ubuntu Redhat 打开EC2控制台,点击启动实例,选择AWS Marketplace Centos.org说明为centos官网镜像 如下图,这种镜像是收费的镜像 Ubuntu ...

  8. SurvivalShooter学习笔记(九.游戏暂停、结束)

    这里先补充一个得分管理器: 玩家得分设置成一个静态变量: public class ScoreManager : MonoBehaviour { public static int score; // ...

  9. JSP内置对象——request 及其响应get和post请求的实例

    request对象客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例.request对象具有请求域,即完成客户端的 ...

  10. Git-fatal: unable to access 'xxx' : Could not resolve host: xxx

    解决办法:(在知乎上找到 确实好用) 1.查询代理 git config --global http.proxy 2.取消代理设置 git config --global --unset http.p ...