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

legend()的一个用法:

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

我们可以这么做:

  1. import numpy as np
  2. x = np.arange(1, 11)
  3. fig = plt.figure(1)
  4. ax1 = plt.subplot(2, 1, 1)
  5. ax2 = plt.subplot(2, 1, 2)
  6. l1, = ax1.plot(x, x*x, 'r') #这里关键哦
  7. l2, = ax2.plot(x, x*x, 'b') # 注意
  8. plt.legend([l1, l2], ['first', 'second'], loc = 'upper right') #其中,loc表示位置的;
  9. plt.show()

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

  1. 'best' : 0, (only implemented for axes legends)(自适应方式)
  2. 'upper right' : 1,
  3. 'upper left' : 2,
  4. 'lower left' : 3,
  5. 'lower right' : 4,
  6. 'right' : 5,
  7. 'center left' : 6,
  8. 'center right' : 7,
  9. 'lower center' : 8,
  10. 'upper center' : 9,
  11. 'center' : 10,

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

其它参数看这里有:

  1. Keyword
  2. Description
  3. loc
  4. a location code
  5. prop
  6. the font property (matplotlib.font_manager.FontProperties 对象)
  7. eg
  8. song_font = matplotlib.font_manager.FontProperties(fname='simsun.ttc', size=8)
  9. fontsize
  10. the font size (和prop互斥,不可同时使用)
  11. markerscale
  12. the relative size of legend markers vs. original
  13. numpoints
  14. the number of points in the legend for line
  15. scatterpoints
  16. the number of points in the legend for scatter plot
  17. scatteryoffsets
  18. a list of yoffsets for scatter symbols in legend
  19. frameon
  20. if True, draw a frame around the legend. If None, use rc
  21. fancybox
  22. if True, draw a frame with a round fancybox. If None, use rc
  23. shadow
  24. if True, draw a shadow behind legend
  25. ncol
  26. number of columns
  27. borderpad
  28. the fractional whitespace inside the legend border
  29. labelspacing
  30. the vertical space between the legend entries
  31. handlelength
  32. the length of the legend handles
  33. handleheight
  34. the length of the legend handles
  35. handletextpad
  36. the pad between the legend handle and text
  37. borderaxespad
  38. the pad between the axes and legend border
  39. columnspacing
  40. the spacing between columns
  41. title
  42. the legend title
  43. bbox_to_anchor
  44. the bbox that the legend will be anchored.
  45. bbox_transform
  46. 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. C++ 类的深拷贝和浅拷贝完美解决

    //类的深拷贝和浅拷贝 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class Poin ...

  2. ubuntu samba 安装

    Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,是一种在局域网上共享文件和打印机的一种通信协议. 1. 安装 sudo apt-get install samba samba-c ...

  3. AWT是Java最早出现的图形界面,但很快就被Swing所取代

    AWT是Java最早出现的图形界面,但很快就被Swing所取代. Swing才是一种真正的图形开发. AWT在不同平台所出现的界面可能有所不同:因为每个OS都有自己的UI组件库,java调用不同系统的 ...

  4. java-I/O File类(5)-Reader和Writer、OutputStreamWriter 、BufferedWriter、字节流和字符流的区别

      标签: outputstreamwriterreader字符file方法 2015-05-14 23:06 469人阅读 评论(0) 收藏 举报  分类: 孙鑫-java基础(16)  I-O(4 ...

  5. InstallSheild 获取系统环境变量,如Desktop路径等

    使用FOLDER_DESKTOP变量获取的桌面路径可能为:C:\Users\Public\Desktop 而不是C:\Users\用户\Desktop Copy and paste the follo ...

  6. Linux环境下Apache配置多个虚拟主机挂载多站点同时运行

    博客地址: http://blog.csdn.net/ClydeKuo/article/details/69569474 这篇博客讲的很详细,很详细.

  7. Spring学习笔记 7.1 Spring MVC起步

    7.1.1 跟踪Spring MVC的请求请求首先到达DispatcherServlet(DispatcherServlet是Spring MVC中的前端控制器):DispatcherServlet的 ...

  8. 从远程(包括ftp,http等协议)地址获取文件流信息

    URL url = new URL("ftp://172.18.251.155:8010/recordsImg/2019-01-28/000008_1548649813267.jpg&quo ...

  9. boost::lexical_cast

    boost::lexical_cast为数值之间的转换(conversion)提供了一揽子方案,比如:将一个字符串"转换成整数123,代码如下: "; int a = lexica ...

  10. 【BZOJ4598】[Sdoi2016]模式字符串 树分治+hash

    [BZOJ4598][Sdoi2016]模式字符串 Description 给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m的模式串s,其中每 ...