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. (LeetCode)旋转数组

    原体描写叙述例如以下: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3 ...

  2. Android:EditText 属性

    Android开发EditText属性 EditText 官网链接 EditText继承关系:View-->TextView-->EditText EditText 部分属性: XML 设 ...

  3. 【Android】11.3 屏幕旋转和场景变换过程中GridView的呈现

    分类:C#.Android.VS2015: 创建日期:2016-02-21 一.简介 实际上,对于布局文件中的View来说,大多数情况下,Android都会自动保存这些状态,并不需要我们都去处理它.这 ...

  4. [Golong]学习笔记(一) 基础知识

    Go编程基础 Go的内置keyword(25个) 不多 break default func interface select case defer go map struct chan else g ...

  5. Flink articles

    http://ictlabs-summer-school.sics.se/2015/slides/flink-advanced.pdf http://henning.kropponline.de/20 ...

  6. 黑客编程教程(六)Windows的关机和重起

    第六节 Windows的关机和重起 很多木马都有远程关机功能,但这并不是一个很好的功能.不过对于入侵服务器,有时需要重起服务器.其实对于关机和重起,只需要调用几个 API函数即可实现. 对于WIN9X ...

  7. FZU Problem 2105 Digits Count

    Problem Description Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: Operati ...

  8. tar压缩解压缩命令详解

    tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...

  9. appium安卓自动化的 常用driver方法封装

    appium安卓自动化的 常用driver方法封装 做安卓自动化的时候,很多方法写起来会造成代码冗余,把这部分封装起来 ,添加到androidUI工具类里,随时可调用 都放在这个类下面: @Compo ...

  10. js实现新闻条目滚动效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...