8000 [Bug]: savefig with bbox_inches does not save image in the figure · Issue #25116 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug]: savefig with bbox_inches does not save image in the figure #25116
Closed as not planned
@chappet

Description

@chappet

Bug summary

The image added in the text bbox is not saved with savefig if setting a bbox.
savefig without option do save the image

Code for reproduction

def GrabTextWithImage():
    
    fig = plt.figure()
    plt.subplot(111)

    #Display Text
    txt = plt.gcf().text(0.10, 0.5, "      ................\n      ................\n      ................"
                      ,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__ == "__main__":
    import numpy
    print (mpl.__version__)

    im = GrabTextWithImage()
    plt.show()
    im.show()

Actual outcome

image

Expected outcome

image

Additional information

No response

Operating system

Win, Python 3.8, Matplotlib 3.6.2

Matplotlib Version

3.6.2

Matplotlib Backend

QtAgg

Python version

3.8.2

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0