Matplotlib文字尺寸

时间:2011-03-16 01:56:03

标签: python matplotlib

是否可以确定matplotlib文本对象的尺寸?如何找到像素的宽度和高度?

由于

编辑:我想我找到了一种方法。我在下面列举了一个例子。

import matplotlib as plt

f = plt.figure()
r = f.canvas.get_renderer()
t = plt.text(0.5, 0.5, 'test')

bb = t.get_window_extent(renderer=r)
width = bb.width
height = bb.height

4 个答案:

答案 0 :(得分:13)

即使在draw()事件之后,我找不到一种方法来获取在绘图上呈现的文本范围。

但这是一种只渲染文本并从中获取各种几何信息的方法:

t = matplotlib.textpath.TextPath((0,0), 'hello', size=9, prop='WingDings')
bb = t.get_extents()

#bb:
#Bbox(array([[  0.759375 ,   0.8915625],
#            [ 30.4425   ,   5.6109375]]))

w = bb.width   #29.683125
h = bb.height  #4.7193749

修改

我一直在玩这个,我有一个不一致的我无法理解。也许别人可以提供帮助。规模似乎已经关闭,我不知道它是dpi问题还是错误或什么,但这个例子几乎可以解释:

import matplotlib
from matplotlib import pyplot as plt
plt.cla()

p = plt.plot([0,10],[0,10])

#ffam = 'comic sans ms'
#ffam = 'times new roman'
ffam = 'impact'
fp = matplotlib.font_manager.FontProperties(
    family=ffam, style='normal', size=30,
    weight='normal', stretch='normal')

txt = 'The quick brown fox'
plt.text(100, 100, txt, fontproperties=fp, transform=None)

pth = matplotlib.textpath.TextPath((100,100), txt, prop=fp)
bb = pth.get_extents()

# why do I need the /0.9 here??
rec = matplotlib.patches.Rectangle(
    (bb.x0, bb.y0), bb.width/0.9, bb.height/0.9, transform=None)
plt.gca().add_artist(rec)

plt.show()

答案 1 :(得分:11)

from matplotlib import pyplot as plt

f = plt.figure()
r = f.canvas.get_renderer()
t = plt.text(0.5, 0.5, 'test')

bb = t.get_window_extent(renderer=r)
width = bb.width
height = bb.height

答案 2 :(得分:1)

感谢您的讨论。在给定数据坐标的宽度和高度的情况下,我可以将答案包装在一个函数中以自动调整文本对象的字体大小(我认为这通常很有用,并认为可以在此处共享)。

与条形边缘重叠的文本示例:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.bar(0.5, 0.5, width=0.5)
text = ax.text(0.5, 0.5, 
                "0.5 (50.00 percent)", 
                va='top', ha='center', 
                fontsize=12)
ax.set_xlim(-0.5, 1.5)

enter image description here

相反,将文本对象的字体大小自动调整为条形宽度:

import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox

def auto_fit_fontsize(text, width, height, fig=None, ax=None):
    '''Auto-decrease the fontsize of a text object.

    Args:
        text (matplotlib.text.Text)
        width (float): allowed width in data coordinates
        height (float): allowed height in data coordinates
    '''
    fig = fig or plt.gcf()
    ax = ax or plt.gca()

    # get text bounding box in figure coordinates
    renderer = fig.canvas.get_renderer()
    bbox_text = text.get_window_extent(renderer=renderer)

    # transform bounding box to data coordinates
    bbox_text = Bbox(ax.transData.inverted().transform(bbox_text))

    # evaluate fit and recursively decrease fontsize until text fits
    fits_width = bbox_text.width < width if width else True
    fits_height = bbox_text.height < height if height else True
    if not all((fits_width, fits_height)):
        text.set_fontsize(text.get_fontsize()-1)
        auto_fit_fontsize(text, width, height, fig, ax)

fig, ax = plt.subplots()
ax.bar(0.5, 0.5, width=0.5)
text = ax.text(0.5, 0.5, 
                "0.5 (50.00 percent)", 
                va='top', ha='center', 
                fontsize=12)
ax.set_xlim(-0.5, 1.5)
auto_fit_fontsize(text, 0.5, None, fig=fig, ax=ax)

enter image description here

答案 3 :(得分:0)

这里是对已接受答案的小修改;

如果要获取轴坐标中的宽度和高度,则可以使用以下内容:

from matplotlib import pyplot as plt

fig, ax = plt.subplots()
r = fig.canvas.get_renderer()
t = ax.text(0.5, 0.5, 'test')

bb = t.get_window_extent(renderer=r).inverse_transformed(ax.transData)
width = bb.width
height = bb.height