2016-09-28 19 views
2

私はテキストの幅を取得する方法を知っている:埋め込みバウンディングボックスを含むmatplotlibテキストの幅を取得する方法は?

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 

をしかし、これは考慮にパディングを取ることはありません。実際、パディングを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 

私の質問は、周囲のボックスの幅をどのように取得できますか?またはFancyBoxPatchの場合、どのようにパディングのサイズを取得できますか?

答えて

3

matplotlib.patches.FancyBboxPatchクラスはmatplotlib.patches.Rectangleクラスに似ていますが、それは長方形の周り ファンシーボックスを描画します。

FancyBboxPatch.get_width()は、(内側)の幅を返す矩形

しかし、FancyBboxPatchget_extents()方法を提供matplotlib.patches.Patchのサブクラスである:

matplotlib.patches.Patch.get_extents()

リターンにBboxオブジェクト定義をパッチの軸に沿った範囲。

このように、何が必要textbox.get_bbox_patch().get_extents().widthです。

+0

ありがとうございました:-)私はこれを探していました。 –

関連する問題