matplotlib画图——绘制多子图、legend的位置
绘制子图
在python上绘制子图,相关的命令有两个
plt.subplot
plt.subplots
两者的区别在于同时创建和逐个创建子图fig, axes = plt.subplots(23)
:即表示一次性在figure上创建成2*3的网格,使用plt.subplot()只能一个一个的添加:
fig = plt.figure()
ax = plt.subplot(231)
ax = plt.subplot(232)
ax = plt.subplot(233)
ax = plt.subplot(234)
ax = plt.subplot(235)
ax = plt.subplot(236)
个人选择使用subplot,在绘制完子图后,因为这里各个子图的图例相同,公用一个图例。
lines, labels = fig.axes[-1].get_legend_handles_labels()
fig.legend( lines, labels,
bbox_to_anchor=(0.74, 0.96),ncol=4, framealpha=1
)
ncol选项指图例中一行有多少个标签,这里设置为4使得标签横向排列。
matplotlib.pyplot.legend 位置设置
loc : str or pair of floats, default: :rc:legend.loc
(‘best’ for axes, ‘upper right’ for figures)
The location of the legend.
The strings
``'upper left', 'upper right', 'lower left', 'lower right'``
place the legend at the corresponding corner of the axes/figure.
The strings
``'upper center', 'lower center', 'center left', 'center right'``
place the legend at the center of the corresponding edge of the
axes/figure.
The string ``'center'`` places the legend at the center of the axes/figure.
The string ``'best'`` places the legend at the location, among the nine
locations defined so far, with the minimum overlap with other drawn
artists. This option can be quite slow for plots with large amounts of
data; your plotting speed may benefit from providing a specific location.
The location can also be a 2-tuple giving the coordinates of the lower-left
corner of the legend in axes coordinates (in which case *bbox_to_anchor*
will be ignored).
For back-compatibility, ``'center right'`` (but no other location) can also
be spelled ``'right'``, and each "string" locations can also be given as a
numeric value:
=============== =============
Location String Location Code
=============== =============
'best' 0
'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 : `.BboxBase`, 2-tuple, or 4-tuple of floats
Box that is used to position the legend in conjunction with *loc*.
Defaults to `axes.bbox` (if called as a method to `.Axes.legend`) or
`figure.bbox` (if `.Figure.legend`). This argument allows arbitrary
placement of the legend.
Bbox coordinates are interpreted in the coordinate system given by
*bbox_transform*, with the default transform
Axes or Figure coordinates, depending on which ``legend`` is called.
If a 4-tuple or `.BboxBase` is given, then it specifies the bbox
``(x, y, width, height)`` that the legend is placed in.
To put the legend in the best location in the bottom right
quadrant of the axes (or figure)::
loc='best', bbox_to_anchor=(0.5, 0., 0.5, 0.5)
A 2-tuple ``(x, y)`` places the corner of the legend specified by *loc* at
x, y. For example, to put the legend's upper right-hand corner in the
center of the axes (or figure) the following keywords can be used::
loc='upper right', bbox_to_anchor=(0.5, 0.5)
legend位置的选择(美观角度)
在存在多个子图的情况下,可以选择第一个子图上。在图片上比较拥挤而图例又比较大时,可以将图例放置到子图外侧,上方或者下方。
python help
授人以鱼不如授人以渔,大家可以学习直接使用Python的help功能,查看一些函数的位置参数和关键字参数。
help("module")
在终端输入help时需要加双引号。
如上面的内容即为在终端进入python环境后,输入help的结果
(unknown_number_jupyter) wendy@wendy-virtual-machine:~/unknown_number/unknown_number_jupyter$ python
Python 3.10.4 (main, Jun 29 2022, 12:14:53) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help("matplotlib.pyplot.legend")
示例
参考文档
绘制子图:https://blog.csdn.net/htuhxf/article/details/82986440?spm=1001.2014.3001.5506
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgaghck
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13