以编程方式控制子图调整#

备注

这个示例主要是为了展示 Matplotlib 中的一些高级概念。

如果你只是需要为标签留出足够的空间,几乎总是更简单且足够好的方法是手动设置子图参数使用 Figure.subplots_adjust,或者使用自动布局机制之一(约束布局指南紧凑布局指南)。

此示例描述了一种用户定义的方式来读取艺术家的大小并相应地设置子图参数。其主要目的是说明一些高级概念,如读取文本位置、处理边界框和变换以及使用 事件。但如果您想自动化布局并需要比紧凑布局和约束布局更大的灵活性,它也可以作为一个起点。

下面,我们收集所有 y 标签的边界框,并将子图的左边界向右移动,以便为所有边界框的并集留出足够的空间。

计算文本边界框有一个需要注意的地方:查询文本边界框(Text.get_window_extent)需要一个渲染器(RendererBase 实例)来计算文本大小。这个渲染器只有在图形绘制后(Figure.draw)才可用。

解决这个问题的方法是将调整逻辑放在绘图回调中。这个函数在图形绘制完成后执行。它现在可以检查子图是否为文本留有足够的空间。如果没有,则更新子图参数并触发第二次绘制。

import matplotlib.pyplot as plt

import matplotlib.transforms as mtransforms

fig, ax = plt.subplots()
ax.plot(range(10))
ax.set_yticks([2, 5, 7], labels=['really, really, really', 'long', 'labels'])


def on_draw(event):
    bboxes = []
    for label in ax.get_yticklabels():
        # Bounding box in pixels
        bbox_px = label.get_window_extent()
        # Transform to relative figure coordinates. This is the inverse of
        # transFigure.
        bbox_fig = bbox_px.transformed(fig.transFigure.inverted())
        bboxes.append(bbox_fig)
    # the bbox that bounds all the bboxes, again in relative figure coords
    bbox = mtransforms.Bbox.union(bboxes)
    if fig.subplotpars.left < bbox.width:
        # Move the subplot left edge more to the right
        fig.subplots_adjust(left=1.1*bbox.width)  # pad a little
        fig.canvas.draw()


fig.canvas.mpl_connect('draw_event', on_draw)

plt.show()
auto subplots adjust

由 Sphinx-Gallery 生成的图库