Skip to content

[Bug]: figimage producing inconsistent results #23399

Open

Description

@swfiua

Bug summary

figimage() is producing inconsistent results.

It appears a transform is sometimes applied to the image and sometimes not.

Code for reproduction

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.

Actual outcome

foo
foo

Expected outcome

I would expect all images to look like the png image, i.e. scaled to the figure size.

Additional information

No response

Operating system

Ubuntu 21.10

Matplotlib Version

3.5.0.dev2201+ge9bd017b2d.d20220707

Matplotlib Backend

'QtAgg'

Python version

Python 3.9.7

Jupyter version

No response

Installation

git checkout

Activity

  1. jklymak commented on Jul 8, 2022

    @jklymak
    Member

    The dpi changes during the lifetime of the figure, so its not surprising that the image changes size. Did you want the resize=True option? https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.figimage.html

  2. swfiua commented on Jul 8, 2022

    @swfiua
    ContributorAuthor

    Thanks, I hadn't considered dpi changes.

    Including resize=True in the call to figimage doesn't seem to change the behaviour.

    I am looking for is a reliable way to put a patch of color on the figure -- in my actual code I am not looking for a patch that covers the whole figure, this is just toy code to demonstrate the issue.

  3. jklymak commented on Jul 8, 2022

    @jklymak
    Member

    it wouldn't surprise me if figimage has fallen out of repair. I guess someone could fix it, but I'm not a particular fan of the API.

    If you just want a patch, then just use Patch, and set the transform to fig.transFig. If you want a background image, I'd just make an axes that fills the whole figure (fig.add_axes([0, 0, 1, 1])).

  4. swfiua commented on Jul 8, 2022

    @swfiua
    ContributorAuthor

    Thanks very much for this, most helpful.
    I've gone with adding a background axes that fills the whole figure and it seems to be working nicely.
    Inclined to agree re: figimage API, given the dpi issue.

  5. reopened this on Jul 8, 2022
  6. jklymak commented on Jul 8, 2022

    @jklymak
    Member

    I think we can re-open. Either the action item is to deprecate figimage or to fix it so at least the resize option works.

  7. carlosgmartin commented on Feb 17, 2025

    @carlosgmartin
    Contributor

    @jklymak I'm also having issues with figimage. Do you still plan to deprecate it? Perhaps it's worth adding a warning + the recommended alternative to the documentation for figimage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions