<h3 dir="auto">Bug summary</h3>
<p dir="auto">The image added in the text bbox is not saved with savefig if setting a bbox.<br>
savefig without option do save the image</p>
<h3 dir="auto">Code for reproduction</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="def GrabTextWithImage():
    
    fig = plt.figure()
    plt.subplot(111)

    #Display Text
    txt = plt.gcf().text(0.10, 0.5, &quot;      ................\n      ................\n      ................&quot;
                      ,bbox={'edgecolor':'black','alpha' : 0.2,'facecolor':'white', 'pad':10, 'linewidth' : 2},
                      verticalalignment='center', horizontalalignment='left', color='black', fontsize=12,zorder = 3)
    

    #Add an image
    an_array = numpy.random.random((20,20)) * 255    
    fig.figimage(an_array,70,240, zorder = 1)

    #Grab around text and return image
    ax = plt.gca()
    r = fig.canvas.get_renderer()
    bb = txt.get_window_extent(r)
    
    transf = ax.transData.inverted()    
    bb_datacoords = bb.transformed(transf)  
    bbox = bb_datacoords.transformed(ax.transData).transformed(fig.dpi_scale_trans.inverted())
    
    plt.axis('off')

    fp = tempfile.TemporaryFile()  
    plt.savefig(fp, bbox_inches = bbox) #BUG : image is not displayed
#     plt.savefig(fp) #Image is displayed
    
    im = Image.open(fp)
    return im

if __name__ == &quot;__main__&quot;:
    import numpy
    print (mpl.__version__)

    im = GrabTextWithImage()
    plt.show()
    im.show()"><pre class="notranslate"><span class="pl-k">def</span> <span class="pl-v">GrabTextWithImage</span>():
    
    <span class="pl-s1">fig</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">figure</span>()
    <span class="pl-s1">plt</span>.<span class="pl-en">subplot</span>(<span class="pl-c1">111</span>)

    <span class="pl-c">#Display Text</span>
    <span class="pl-s1">txt</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">gcf</span>().<span class="pl-en">text</span>(<span class="pl-c1">0.10</span>, <span class="pl-c1">0.5</span>, <span class="pl-s">"      ................<span class="pl-cce">\n</span>      ................<span class="pl-cce">\n</span>      ................"</span>
                      ,<span class="pl-s1">bbox</span><span class="pl-c1">=</span>{<span class="pl-s">'edgecolor'</span>:<span class="pl-s">'black'</span>,<span class="pl-s">'alpha'</span> : <span class="pl-c1">0.2</span>,<span class="pl-s">'facecolor'</span>:<span class="pl-s">'white'</span>, <span class="pl-s">'pad'</span>:<span class="pl-c1">10</span>, <span class="pl-s">'linewidth'</span> : <span class="pl-c1">2</span>},
                      <span class="pl-s1">verticalalignment</span><span class="pl-c1">=</span><span class="pl-s">'center'</span>, <span class="pl-s1">horizontalalignment</span><span class="pl-c1">=</span><span class="pl-s">'left'</span>, <span class="pl-s1">color</span><span class="pl-c1">=</span><span class="pl-s">'black'</span>, <span class="pl-s1">fontsize</span><span class="pl-c1">=</span><span class="pl-c1">12</span>,<span class="pl-s1">zorder</span> <span class="pl-c1">=</span> <span class="pl-c1">3</span>)
    

    <span class="pl-c">#Add an image</span>
    <span class="pl-s1">an_array</span> <span class="pl-c1">=</span> <span class="pl-s1">numpy</span>.<span class="pl-s1">random</span>.<span class="pl-en">random</span>((<span class="pl-c1">20</span>,<span class="pl-c1">20</span>)) <span class="pl-c1">*</span> <span class="pl-c1">255</span>    
    <span class="pl-s1">fig</span>.<span class="pl-en">figimage</span>(<span class="pl-s1">an_array</span>,<span class="pl-c1">70</span>,<span class="pl-c1">240</span>, <span class="pl-s1">zorder</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span>)

    <span class="pl-c">#Grab around text and return image</span>
    <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">gca</span>()
    <span class="pl-s1">r</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-s1">canvas</span>.<span class="pl-en">get_renderer</span>()
    <span class="pl-s1">bb</span> <span class="pl-c1">=</span> <span class="pl-s1">txt</span>.<span class="pl-en">get_window_extent</span>(<span class="pl-s1">r</span>)
    
    <span class="pl-s1">transf</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-s1">transData</span>.<span class="pl-en">inverted</span>()    
    <span class="pl-s1">bb_datacoords</span> <span class="pl-c1">=</span> <span class="pl-s1">bb</span>.<span class="pl-en">transformed</span>(<span class="pl-s1">transf</span>)  
    <span class="pl-s1">bbox</span> <span class="pl-c1">=</span> <span class="pl-s1">bb_datacoords</span>.<span class="pl-en">transformed</span>(<span class="pl-s1">ax</span>.<span class="pl-s1">transData</span>).<span class="pl-en">transformed</span>(<span class="pl-s1">fig</span>.<span class="pl-s1">dpi_scale_trans</span>.<span class="pl-en">inverted</span>())
    
    <span class="pl-s1">plt</span>.<span class="pl-en">axis</span>(<span class="pl-s">'off'</span>)

    <span class="pl-s1">fp</span> <span class="pl-c1">=</span> <span class="pl-s1">tempfile</span>.<span class="pl-v">TemporaryFile</span>()  
    <span class="pl-s1">plt</span>.<span class="pl-en">savefig</span>(<span class="pl-s1">fp</span>, <span class="pl-s1">bbox_inches</span> <span class="pl-c1">=</span> <span class="pl-s1">bbox</span>) <span class="pl-c">#BUG : image is not displayed</span>
<span class="pl-c">#     plt.savefig(fp) #Image is displayed</span>
    
    <span class="pl-s1">im</span> <span class="pl-c1">=</span> <span class="pl-v">Image</span>.<span class="pl-en">open</span>(<span class="pl-s1">fp</span>)
    <span class="pl-k">return</span> <span class="pl-s1">im</span>

<span class="pl-k">if</span> <span class="pl-s1">__name__</span> <span class="pl-c1">==</span> <span class="pl-s">"__main__"</span>:
    <span class="pl-k">import</span> <span class="pl-s1">numpy</span>
    <span class="pl-en">print</span> (<span class="pl-s1">mpl</span>.<span class="pl-s1">__version__</span>)

    <span class="pl-s1">im</span> <span class="pl-c1">=</span> <span class="pl-v">GrabTextWithImage</span>()
    <span class="pl-s1">plt</span>.<span class="pl-en">show</span>()
    <span class="pl-s1">im</span>.<span class="pl-en">show</span>()</pre></div>
<h3 dir="auto">Actual outcome</h3>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/1446742/215844148-8deaf81c-dea5-489f-96a0-26e16734407f.png"><img src="https://user-images.githubusercontent.com/1446742/215844148-8deaf81c-dea5-489f-96a0-26e16734407f.png" alt="image" style="max-width: 100%;"></a></p>
<h3 dir="auto">Expected outcome</h3>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/1446742/215844238-3718c2f4-c82b-4525-83fc-e1daf343f75d.png"><img src="https://user-images.githubusercontent.com/1446742/215844238-3718c2f4-c82b-4525-83fc-e1daf343f75d.png" alt="image" style="max-width: 100%;"></a></p>
<h3 dir="auto">Additional information</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Operating system</h3>
<p dir="auto">Win, Python 3.8, Matplotlib 3.6.2</p>
<h3 dir="auto">Matplotlib Version</h3>
<p dir="auto">3.6.2</p>
<h3 dir="auto">Matplotlib Backend</h3>
<p dir="auto">QtAgg</p>
<h3 dir="auto">Python version</h3>
<p dir="auto">3.8.2</p>
<h3 dir="auto">Jupyter version</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Installation</h3>
<p dir="auto">pip</p>
<h3 dir="auto">Bug summary</h3>
<p dir="auto">figimage() is producing inconsistent results.</p>
<p dir="auto">It appears a transform is sometimes applied to the image and sometimes not.</p>
<h3 dir="auto">Code for reproduction</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from matplotlib import pyplot
plt = pyplot
import numpy as np

fig = pyplot.figure()
ax = fig.subplots()


fig_bbox = fig.bbox
width, height = fig_bbox.width, fig_bbox.height

img = np.zeros((int(height), int(width), 3))
img += (127, 0, 0)

fig.figimage(img, 0, 0, zorder=-1)

# show the figure, image is not scaled as I would expect
plt.show()

# save as a png, image is scaled as I would expect
fig.savefig('foo.png')

# save as an svg, image matches the one in the FigureCanvas window.
fig.savefig('foo.svg')

# NB reversing the order of the two savefig calls produces images
# matching the FigureCanvas version."><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">matplotlib</span> <span class="pl-k">import</span> <span class="pl-s1">pyplot</span>
<span class="pl-s1">plt</span> <span class="pl-c1">=</span> <span class="pl-s1">pyplot</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-s1">fig</span> <span class="pl-c1">=</span> <span class="pl-s1">pyplot</span>.<span class="pl-en">figure</span>()
<span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">subplots</span>()


<span class="pl-s1">fig_bbox</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-s1">bbox</span>
<span class="pl-s1">width</span>, <span class="pl-s1">height</span> <span class="pl-c1">=</span> <span class="pl-s1">fig_bbox</span>.<span class="pl-s1">width</span>, <span class="pl-s1">fig_bbox</span>.<span class="pl-s1">height</span>

<span class="pl-s1">img</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros</span>((<span class="pl-en">int</span>(<span class="pl-s1">height</span>), <span class="pl-en">int</span>(<span class="pl-s1">width</span>), <span class="pl-c1">3</span>))
<span class="pl-s1">img</span> <span class="pl-c1">+=</span> (<span class="pl-c1">127</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>)

<span class="pl-s1">fig</span>.<span class="pl-en">figimage</span>(<span class="pl-s1">img</span>, <span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-s1">zorder</span><span class="pl-c1">=</span><span class="pl-c1">-</span><span class="pl-c1">1</span>)

<span class="pl-c"># show the figure, image is not scaled as I would expect</span>
<span class="pl-s1">plt</span>.<span class="pl-en">show</span>()

<span class="pl-c"># save as a png, image is scaled as I would expect</span>
<span class="pl-s1">fig</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'foo.png'</span>)

<span class="pl-c"># save as an svg, image matches the one in the FigureCanvas window.</span>
<span class="pl-s1">fig</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'foo.svg'</span>)

<span class="pl-c"># NB reversing the order of the two savefig calls produces images</span>
<span class="pl-c"># matching the FigureCanvas version.</span></pre></div>
<h3 dir="auto">Actual outcome</h3>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/251611/177882811-6c3cbeff-a885-4a67-bf43-65f038b44417.png"><img src="https://user-images.githubusercontent.com/251611/177882811-6c3cbeff-a885-4a67-bf43-65f038b44417.png" alt="foo" style="max-width: 100%;"></a><br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/251611/177882813-b980da1c-0744-4f7e-b422-e77fc1d01df6.svg"><img src="https://user-images.githubusercontent.com/251611/177882813-b980da1c-0744-4f7e-b422-e77fc1d01df6.svg" alt="foo" style="max-width: 100%;"></a></p>
<h3 dir="auto">Expected outcome</h3>
<p dir="auto">I would expect all images to look like the png image, i.e. scaled to the figure size.</p>
<h3 dir="auto">Additional information</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Operating system</h3>
<p dir="auto">Ubuntu 21.10</p>
<h3 dir="auto">Matplotlib Version</h3>
<p dir="auto">3.5.0.dev2201+ge9bd017b2d.d20220707</p>
<h3 dir="auto">Matplotlib Backend</h3>
<p dir="auto">'QtAgg'</p>
<h3 dir="auto">Python version</h3>
<p dir="auto">Python 3.9.7</p>
<h3 dir="auto">Jupyter version</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Installation</h3>
<p dir="auto">git checkout</p>