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

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,可以通过共用例的方式将子图添加到同一个中。在matplotlib库中,可以使用`legend()`函数来创建例。下面介绍具体的步骤: 1. 首先,需要将形划分为多个子图,可以使用`subplot()`函数来创建子图。具体来说,通过指定`nrows`和`ncols`参数,可以指定行数和列数,然后使用`plt.subplot()`来选择不同位置子图。 2. 在每个子图绘制形,例如使用`plot()`函数绘制线。可以通过传递`label`参数来指定每个形的标签,这些标签将在例中显示。 3. 在绘制完所有子图后,通过`plt.legend()`函数来创建例。可以使用`loc`参数来指定例的位置,例如`loc='best'`表示自动选择最佳位置。 值得注意的是,在添加子图之前,需要调用`plt.figure()`函数来创建一个新的形。 下面是一个简单的示例代码: ```python import matplotlib.pyplot as plt # 创建一个新的形 plt.figure() # 创建两个子图,一个在左上角,一个在右上角 plt.subplot(2, 1, 1) plt.plot([1, 2, 3], [1, 2, 3], label='Line 1') plt.legend() plt.subplot(2, 1, 2) plt.plot([1, 2, 3], [3, 2, 1], label='Line 2') plt.legend() # 创建例并显示 plt.legend(loc='best') plt.show() ``` 在上面的代码中,使用`subplot(2, 1, 1)`创建了一个2行1列的子图,位于第一行第一列的位置。在该子图绘制了一个线,并通过`label`参数指定了该形的标签。然后调用`subplot(2, 1, 2)`创建了位于第二行的子图,并在其中绘制了另一个线。 最后,调用`plt.legend()`函数创建了一个共用的例,并将其位置设置为最佳。可以通过`plt.show()`来显示形。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值