Matplotlib文本边界框尺寸 [英] Matplotlib text bounding box dimensions

查看:387
本文介绍了Matplotlib文本边界框尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get the position and dimensions of a text instance in matplotlib world units (not screen pixels), with the intention of calculating and preventing text overlaps.

我正在Mac OSX 10.9.3,Python 2.7.5,matplotlib 1.3.1上进行开发.

I'm developing on Mac OSX 10.9.3, Python 2.7.5, matplotlib 1.3.1.

t 为文本实例.

  1. t .get_window_extent (渲染器):

  1. t.get_window_extent(renderer):

这将获得以像素为单位的边界框尺寸,并且我需要世界坐标(在我的情况下规格化为-1.0和1.0之间).

This gets bounding box dimensions in pixels, and I need world coordinates (normalized between -1.0 and 1.0 in my case).

t ._ get_bbox_patch():

t._get_bbox_patch():

t = ax.text(x, y, text_string, prop_dict, bbox=dict(facecolor='red', alpha=0.5, boxstyle='square'))
print t._get_bbox_patch()

当我执行以上序列时,输出为FancyBboxPatchFancyBboxPatch(0,0;1x1).在我生成的图像中,文本实例使用红色边框正确渲染,因此输出使我认为FancyBbox已实例化,但直到渲染时才实际填充实际尺寸.

When I execute the above sequence, the output is FancyBboxPatchFancyBboxPatch(0,0;1x1). In the image I produce, the text instance is rendered properly with a red bounding box, so that output leads me to think that the FancyBbox is instantiated but not actually populated with real dimensions until render time.

因此,如何获得文本实例边框的位置和尺寸,该坐标和尺寸与我传递的 x y 参数使用的坐标系统单位相同到ax.text(...)?

So, how can I get the position and dimensions of the text instance's bounding box in the same coord system units that I used for the x and y parameters I passed to ax.text(...)?

推荐答案

这可能会有所帮助.

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的倒数.

This should give what you want. If you want to have the coordinates in terms of figure coordinates (0..1,0..1), then use the inverse of ax.transAxes.

但是,此解决方案有一个小问题. matplotlib文档摘录:

However, there is a small catch in this solution. An excerpt from the matplotlib documentation:

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

用于绘制文本大小的RendererBase实例在绘制图形(draw())之前是未知的.绘制窗口并且文本实例知道其渲染器之后,可以调用get_window_extent().

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

So, before the figure is really drawn, there seems to be no way to find out the text size.

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

BTW, you may have noticed that the Bbox instances have method overlaps which may be used to find out whether the Bbox overlaps with another one (bb1.overlaps(bb2)). This may be useful in some cases, but it does not answer the question "how much".

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

If you have rotated texts, you will have hard time seeing if they overlap, but that you probably already know.

这篇关于Matplotlib文本边界框尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆