我有一个字幕对象,有时会使用 Matplotlib 的内置包装功能进行包装。但是,当试图获取字幕的高度时,我似乎总是得到与一行相对应的高度。我哪里错了?这就是我正在尝试的:
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
fig = Figure((4, 4))
FigureCanvas(fig)
text_1 = "I'm a short text"
text_2 = "I'm a longer text that will be wrapped autoamtically by Matplotlib, using wrap=True"
title = fig.suptitle(text_1, wrap=True)
fig.canvas.draw() # Draw text to find out how big it is
bbox = title.get_window_extent()
print(bbox.width) # 105
print(bbox.height) # 14
title = fig.suptitle(text_2, wrap=True)
fig.canvas.draw() # Draw text to find out how big it is
bbox = title.get_window_extent()
print(bbox.width) # 585 <-- This looks about right
print(bbox.height) # Still 14 even though this time the text is wrapped!
对象也会发生同样的事情Text(使用类似fig.text(0.5, 0.5, text_1, wrap=True).