matplotlib画图

您所在的位置:网站首页 Python画两张图在斜对角位置 matplotlib画图

matplotlib画图

2024-07-11 04:41| 来源: 网络整理| 查看: 265

绘制子图

在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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3