Skip to content

bbox images do not get placed properly when figure is saved to png or pdf #3918

@breedlun

Description

@breedlun

When I create a figure using bbox images, everything looks fine, but when I save the figure, the images move. Here is an example

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.image import BboxImage
from matplotlib.transforms import Bbox, TransformedBbox

#Generate the image
im = np.random.rand(10, 10) * 255.0

#Set up figure
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
ax.set_xlim(-2,24)
ax.set_ylim(0,10)

#Create image that transforms automatically
bbox1 = Bbox.from_extents([0,0,10,10])
trans_bbox1 = TransformedBbox(bbox1, ax.transData)
bbox_image1 = BboxImage(trans_bbox1)
bbox_image1.set_data(im)
ax.add_image(bbox_image1)

#Create image that does not transform
bbox2 = Bbox.from_extents([12,0,22,10])
trans_bbox2 = bbox2.transformed(ax.transData)
bbox_image2 = BboxImage(trans_bbox2)
bbox_image2.set_data(im)
ax.add_image(bbox_image2)

#Save the result
fig.savefig('png_test.png')
fig.savefig('pdf_test.pdf')

If I take a screenshot of the figure window, it looks like this
screenshot
which is what I expect. However, when I look at the 'png_test.png', the second image that did not use the TransformedBbox moves, as shown below
png_test
In the pdf, both images move, as shown below
pdf_test

In case it matters, I am using the Qt4Agg interactive backend with the Spyder IDE. My matplotlib version is 1.4.0.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions