Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: figimage producing inconsistent results #23399

Open
swfiua opened this issue Jul 7, 2022 · 5 comments
Open

[Bug]: figimage producing inconsistent results #23399

swfiua opened this issue Jul 7, 2022 · 5 comments

Comments

@swfiua
Copy link
Contributor

swfiua commented Jul 7, 2022

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

@jklymak
Copy link
Member

jklymak commented Jul 8, 2022

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

@jklymak jklymak added the status: needs clarification Issues that need more information to resolve. label Jul 8, 2022
@swfiua
Copy link
Contributor Author

swfiua commented Jul 8, 2022

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.

@jklymak
Copy link
Member

jklymak commented Jul 8, 2022

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])).

@swfiua
Copy link
Contributor Author

swfiua commented Jul 8, 2022

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.

@swfiua swfiua closed this as completed Jul 8, 2022
@jklymak jklymak reopened this Jul 8, 2022
@jklymak
Copy link
Member

jklymak commented Jul 8, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants