legend()的一个用法:

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

我们可以这么做:

import matplotlib.pyplot as plt
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中的legend()—显示图例

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

  2. 从多个XML文档中读取数据用于显示webapi帮助文档

    前言: 你先得知道HelpPageConfig文件,不知道说明你现在不需要这个,所以下文就不用看了,等知道了再看也不急.当然如果你很知道这个,下文也不用看了,因为你会了. 方法一: new XmlDo ...

  3. matplotlib中subplots的用法

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

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

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

  5. python 2: 解决python中的plot函数的图例legend不能显示中文问题

     问题: 图像标题.横纵坐标轴的标签都能显示中文名字,但是图例就是不能显示中文,怎么解决呢?  解决: plt.figure() plt.title(u'训练性能', fontproperties=f ...

  6. Matplotlib中中文不显示问题

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

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

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

  8. Matplotlib 中常见的图形

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

  9. plot与legend画图与图例

    画图与图例: legend(x, y = NULL, legend, fill = NULL, col = par("col"), border = "black&quo ...

随机推荐

  1. /usr/local/ssl/lib/libssl.a: error adding symbols: Bad value

    一.背景 编译第三方库A的时候提示依赖openssl库. 二.编译openssl库 去官网下载最新版本的openssl-1.0.2l.tar.gztar -zxvf openssl-1.0.2l.ta ...

  2. 【服务器防护】linux 如何查看防火墙是否开启

    service iptables status可以查看到iptables服务的当前状态.但是即使服务运行了,防火墙也不一定起作用,你还得看防火墙规则的设置 iptables -L在此说一下关于启动和关 ...

  3. BBED标记坏块以及修复坏块

    创建測试表: ZBDBA@orcl11g>select * from zbdba; NAME -------------------------------------------------- ...

  4. CentOS使用EPEL YUM源

    刚刚开始学习centos,感觉很不适应,因为好多软件,yum里面居然没有,让我很不爽,一阵狂搜. 原来centos还有一个epel源,果然,不熟悉的东东,处处是坑啊! EPEL (Extra Pack ...

  5. 【Android】将Xamarin For VS升级为4.0.1.145版

    分类:C#.Android.VS2015 创建日期:2016-03-18 一.卸载原来安装的Xamarin for VS 4.0.0.1717版 下面是Xamarin for VS发布的版本简介: - ...

  6. 6.3 cmath--数学函数

    本模块提供了处理复数的数学函数.因此这些函数接受整数.浮点数或者复数作为參数. 6.3.1 与极坐标相互转换的函数 在Python里表示一个复数z,实部使用z.real表示,虚部使用z.imag,能够 ...

  7. Django:Django Rest Framework

    Django Rest Framework   一. 什么是RESTful REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称 ...

  8. android Handlerr.removeCallbacksAndMessages(null)的妙用

    今天在阅读代码发现了android  Handlerr.removeCallbacksAndMessages(null)代码 在ondestory()调用,之前没用过,那想弄懂咋办,查api: pub ...

  9. %02x和%2x区别

    %02x  (x代表以十六进制形式输出,02代表不足两位,前面补0输出,如果超过两位,则以实际输出) 例: #include <stdio.h> #include <stdint.h ...

  10. QT学习记录(1)-控件 QPushButton, QLineEdit, QLabel, QHBoxLayout, QGridLayout

    1.一个简单的QT程序(QPushButton) /* 应用程序抽象类 */ #include <QApplication> /*窗口类*/ #include <QWidget> ...