How to get the width of a matplotlib text, including the padded bounding box?
<p>I know how to get the width of the text:</p>

<pre><code>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
</code></pre>

<p>However, this does not take into account the padding. Indeed, if I set the padding to 0, I get the same width.</p>

<pre><code>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
</code></pre>

<p>My question is: how can I get the width of the surrounding box? or how can I get the size of the padding in the case of a FancyBoxPatch? </p>

<blockquote>
  <p><a href="http://matplotlib.org/api/patches_api.html#matplotlib.patches.FancyBboxPatch" rel="nofollow"><code>matplotlib.patches.FancyBboxPatch</code></a> class is similar to <a href="http://matplotlib.org/api/patches_api.html#matplotlib.patches.Rectangle" rel="nofollow"><code>matplotlib.patches.Rectangle</code></a> class, but it draws
  a fancy box around the rectangle.</p>
  
  <p><a href="http://matplotlib.org/api/patches_api.html#matplotlib.patches.FancyBboxPatch.get_width" rel="nofollow"><code>FancyBboxPatch.get_width()</code></a> returns the width of the (inner) rectangle</p>
</blockquote>

<p>However, <a href="http://matplotlib.org/api/patches_api.html#matplotlib.patches.FancyBboxPatch" rel="nofollow"><code>FancyBboxPatch</code></a> is a subclass of <a href="http://matplotlib.org/api/patches_api.html#matplotlib.patches.Patch" rel="nofollow"><code>matplotlib.patches.Patch</code></a> which provides a <a href="http://matplotlib.org/api/patches_api.html#matplotlib.patches.Patch.get_extents" rel="nofollow"><code>get_extents()</code></a> method:</p>

<blockquote>
  <p><a href="http://matplotlib.org/api/patches_api.html#matplotlib.patches.Patch.get_extents" rel="nofollow"><code>matplotlib.patches.Patch.get_extents()</code></a></p>
  
  <p>Return a Bbox object defining the axis-aligned extents of the Patch.</p>
</blockquote>

<p>Thus, what you need is <strong><code>textbox.get_bbox_patch().get_extents().width</code></strong>.</p>

python matplotlib