源自  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. 指数族分布(Exponential Families of Distributions)

    指数族分布是一大类分布,基本形式为: T(x)是x的充分统计量(能为相应分布提供足够信息的统计量) 为了满足归一化条件,有: 可以看出,当T(x)=x时,e^A(theta)是h(x)的拉普拉斯变换. ...

  2. google cloud之查看任务任务过程

    点击侧边栏的logging

  3. CI -- $this->load->library()详解

    我第一次加载失败,原来是文件名和类名不同的原因,先总结关于CI加载你自己的类文件注意事项: 1.第三方加载文件应放在application/libraries文件下 2.文件名和类名应该相同,并且首字 ...

  4. 【BZOJ】1609: [Usaco2008 Feb]Eating Together麻烦的聚餐(dp+被坑)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1609 首先我不得不说,我被这题坑了.题目前边没有说可以不需要3种牛都有啊!!!!!!!!然后我一直在 ...

  5. 自定义View总结

    写的很好,代你分析原码,关于 View Measure 测量机制,让我一次把话说完

  6. 面试题思考:解释一下什么叫AOP(面向切面编程)

    这种在运行时,动态地将代码切入到类的指定方法.指定位置上的编程思想就是面向切面的编程. AOP是Spring提供的关键特性之一.AOP即面向切面编程,是OOP编程的有效补充. 使用AOP技术,可以将一 ...

  7. 使用js里面的迭代器filter实现数组去重

    实现数组去重的方法很多,最原始的方法是一个值一个值的去遍历,写到空数组里面: let r=[],arr = ['a', 'b', 'c', 'a']; for(var i=0,len=arr.leng ...

  8. 创建4个线程,两个对j加一,两个对j减一(j两同两内)

    package multithread; public class MyThread { //j变量私有 private int j; //同步的+1方法 private synchronized v ...

  9. Javascript实现图片的预加载的完整实现

    图片预加载是web开发中一种应用相当广泛的技术,比如我们在做图片翻转显示等特效的时候,为了让图片在转换的时候不出现等待,我们最好是先让图片下载到本地,然后在继续执行后续的操作.今天我们将来实现一个完整 ...

  10. 延迟加载JavaScript

    上代码: 这段代码放到HTML文档的</body>标签之前(靠近HTML文档底部).外部脚本的名称为defer.js. <script type="text/javascr ...