Python Matplotlib文本边界框尺寸 我想做的是:

Python Matplotlib文本边界框尺寸 我想做的是:,python,macos,matplotlib,Python,Macos,Matplotlib,我希望以matplotlib世界单位(而不是屏幕像素)获取文本实例的位置和尺寸,以便计算和防止文本重叠 我正在Mac OSX 10.9.3、Python 2.7.5、matplotlib 1.3.1上开发 我所尝试的: 让t成为一个文本实例 : 这将获得以像素为单位的边界框尺寸,我需要世界坐标(在我的例子中标准化为-1.0和1.0) t、 _get_bbox_patch(): 当我执行上述序列时,输出是FancyBoxPatchFancyBoxPatch(0,0;1x1)。在我生成的图像中,文本

我希望以matplotlib世界单位(而不是屏幕像素)获取文本实例的位置和尺寸,以便计算和防止文本重叠

我正在Mac OSX 10.9.3、Python 2.7.5、matplotlib 1.3.1上开发

我所尝试的: 让t成为一个文本实例

  • :

    这将获得以像素为单位的边界框尺寸,我需要世界坐标(在我的例子中标准化为-1.0和1.0)

  • t、 _get_bbox_patch():

    当我执行上述序列时,输出是
    FancyBoxPatchFancyBoxPatch(0,0;1x1)
    。在我生成的图像中,文本实例使用红色边界框正确渲染,因此输出使我认为Fancybox已实例化,但在渲染时才实际填充真实尺寸

  • 因此,如何在传递给ax.text(…)?

    的x和y参数中使用的坐标系单位中获取文本实例边界框的位置和尺寸,这可能会有所帮助

    import matplotlib.pyplot as plt
    
    f = plt.figure()
    ax = f.add_subplot(111)
    ax.plot([0,10], [4,0])
    t = ax.text(3.2, 2.1, "testing...")
    
    # get the inverse of the transformation from data coordinates to pixels
    transf = ax.transData.inverted()
    bb = t.get_window_extent(renderer = f.canvas.renderer)
    bb_datacoords = bb.transformed(transf)
    
    # Bbox('array([[ 3.2       ,  2.1       ],\n       [ 4.21607125,  2.23034396]])')
    
    这应该是你想要的。如果您想用图形坐标(0..1,0..1)表示坐标,则使用
    ax.transAxes
    的倒数

    然而,这个解决方案有一个小问题。
    matplotlib
    文档节选:

    任何文本实例都可以在窗口坐标中报告其范围(负x坐标在窗口外),但存在摩擦

    用于计算文本大小的RenderBase实例在绘制图形(draw())之前是未知的。绘制窗口并且文本实例知道其渲染器后,可以调用get\u window\u extent()

    因此,在真正绘制图形之前,似乎没有办法确定文本大小

    顺便说一句,您可能已经注意到,
    Bbox
    实例具有方法
    overlaps
    ,该方法可用于确定
    Bbox
    是否与另一个重叠(
    bb1.overlaps(bb2)
    )。这在某些情况下可能有用,但它不能回答“多少”的问题


    如果您旋转了文本,您将很难看到它们是否重叠,但您可能已经知道了。

    有点晚了,但下面是另一个示例,它显示了如何以数据坐标/单位获取文本对象的边界框。它还绘制在文本周围获得的边界框,用于其视觉表示

    import matplotlib.pyplot as plt
    
    # some example plot
    plt.plot([1,2,3], [2,3,4])
    
    t = plt.text(1.1, 3.1, "my text", fontsize=18)
    
    # to get the text bounding box 
    # we need to draw the plot
    plt.gcf().canvas.draw()
    
    
    # get bounding box of the text 
    # in the units of the data
    bbox = t.get_window_extent()\
        .inverse_transformed(plt.gca().transData)
    
    
    print(bbox)
    # prints: Bbox(x0=1.1, y0=3.0702380952380954, x1=1.5296875, y1=3.2130952380952382)
    
    
    # plot the bounding box around the text
    plt.plot([bbox.x0, bbox.x0, bbox.x1, bbox.x1, bbox.x0],
             [bbox.y0, bbox.y1, bbox.y1, bbox.y0, bbox.y0])
    
    plt.show()
    

    这对我来说非常有效,唯一的问题是我必须使用
    f.canvas.get\u renderer()
    而不是
    f.canvas.renderer
    import matplotlib.pyplot as plt
    
    # some example plot
    plt.plot([1,2,3], [2,3,4])
    
    t = plt.text(1.1, 3.1, "my text", fontsize=18)
    
    # to get the text bounding box 
    # we need to draw the plot
    plt.gcf().canvas.draw()
    
    
    # get bounding box of the text 
    # in the units of the data
    bbox = t.get_window_extent()\
        .inverse_transformed(plt.gca().transData)
    
    
    print(bbox)
    # prints: Bbox(x0=1.1, y0=3.0702380952380954, x1=1.5296875, y1=3.2130952380952382)
    
    
    # plot the bounding box around the text
    plt.plot([bbox.x0, bbox.x0, bbox.x1, bbox.x1, bbox.x0],
             [bbox.y0, bbox.y1, bbox.y1, bbox.y0, bbox.y0])
    
    plt.show()