我知道如何获取文本的宽度:
import matplotlib.pyplot as plt
from matplotlib.patches import BoxStyle
xpos, ypos = 0, 0
text = 'blah blah'
boxstyle = BoxStyle("Round", pad=1)
props = {'boxstyle': boxstyle,
'facecolor': 'white',
'linestyle': 'solid',
'linewidth': 1,
'edgecolor': 'black'}
textbox = plt.text(xpos, ypos, text, bbox=props)
plt.show()
textbox.get_bbox_patch().get_width() # 54.121092459652573
Run Code Online (Sandbox Code Playgroud)
但是,这并没有考虑到填充。事实上,如果我将填充设置为 0,我会得到相同的宽度。
boxstyle = BoxStyle("Round", pad=0)
props = {'boxstyle': boxstyle,
'facecolor': 'white',
'linestyle': 'solid',
'linewidth': 1,
'edgecolor': 'black'}
textbox = plt.text(xpos, ypos, text, bbox=props)
plt.show()
textbox.get_bbox_patch().get_width() # 54.121092459652573
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何获得周围框的宽度?或者在 FancyBoxPatch 的情况下如何获得填充的大小?
matplotlib.patches.FancyBboxPatchclass 与 class 类似matplotlib.patches.Rectangle,但它在矩形周围绘制了一个漂亮的框。
FancyBboxPatch.get_width()返回(内部)矩形的宽度
但是,FancyBboxPatch它的一个子类matplotlib.patches.Patch提供了一个get_extents()方法:
matplotlib.patches.Patch.get_extents()返回定义 Patch 轴对齐范围的 Bbox 对象。
因此,您需要的是textbox.get_bbox_patch().get_extents().width.
| 归档时间: |
|
| 查看次数: |
3659 次 |
| 最近记录: |