Skip to content

Fix occasional misalignment in reported mouse position (also fixes a bug with canvas height)#32038

Open
ayshih wants to merge 3 commits into
matplotlib:mainfrom
ayshih:mouseover_misalignment
Open

Fix occasional misalignment in reported mouse position (also fixes a bug with canvas height)#32038
ayshih wants to merge 3 commits into
matplotlib:mainfrom
ayshih:mouseover_misalignment

Conversation

@ayshih

@ayshih ayshih commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

PR summary

Due to floating-point precision, a figure may self-report a height that is slightly below an integer (by one floating-point tick). When this happens, there are (at least) two bugs:

>>> import matplotlib.pyplot as plt
>>> fig = plt.figure(figsize=(1, 2.03), dpi=100)
>>> print(*fig.bbox.size)
100.0 202.99999999999997
>>> fig.canvas.draw()
>>> print(fig.canvas.buffer_rgba().shape[1::-1])
(100, 202)

This PR:

  • Fixes FigureCanvasBase.get_width_height() so that it accounts for floating-point precision before truncating to integers so that the canvas will be the intended size.
  • Switch backends to expressly request the canvas size in integral pixels (where appropriate) rather than internally calculate it from the figure size.

Fixes #27570
Fixes #15363

AI Disclosure

No AI was used

PR checklist

@timhoffm

Copy link
Copy Markdown
Member

I’m wondering whether we should make an explicit concept out of this. While figure size can be any float, the canvas - at least for raster backends - must be integer. So it seems we should rather take the size from the canvas? Possibly https://matplotlib.org/stable/api/backend_bases_api.html#matplotlib.backend_bases.FigureCanvasBase.get_width_height

@ayshih ayshih force-pushed the mouseover_misalignment branch from 6392c2b to f7eb4c0 Compare July 13, 2026 16:38
@ayshih ayshih changed the title Fix occasional misalignment in reported mouse position Fix occasional misalignment in reported mouse position and its associated data Jul 13, 2026
@ayshih ayshih changed the title Fix occasional misalignment in reported mouse position and its associated data Fix occasional misalignment in reported mouse position Jul 13, 2026
@ayshih

ayshih commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Per @timhoffm's suggestion, I changed the approach of this PR: I have fixed FigureCanvasBase.get_width_height() and then made the backends actually use that method.

@ayshih ayshih force-pushed the mouseover_misalignment branch from f7eb4c0 to a594bbf Compare July 13, 2026 17:20
@ayshih ayshih force-pushed the mouseover_misalignment branch from a594bbf to 4be3326 Compare July 13, 2026 17:29
Comment thread lib/matplotlib/backend_bases.py Outdated
backend.
"""
return tuple(int(size / (1 if physical else self.device_pixel_ratio))
# Due to floating-point precision, round up width or height if either is very

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s possibly a good idea to add to the docstring that rounding is used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a note in the docstring

@QuLogic

QuLogic commented Jul 13, 2026

Copy link
Copy Markdown
Member

Given how this has changed, I guess I should point out #8265 and ask whether it is something that correlates with this PR.

@ayshih

ayshih commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Ah, indeed, this PR fixes #15363 and its duplicates. I don't know if #8265 still breaks existing tests, but in contrast this PR is tightly targeted because it affects only those situations when the height is one floating-point tick low rather than rounding all heights.

@ayshih ayshih changed the title Fix occasional misalignment in reported mouse position Fix occasional misalignment in reported mouse position (also fixes a bug with canvas height) Jul 13, 2026
@ayshih ayshih marked this pull request as ready for review July 13, 2026 19:41
@ayshih ayshih force-pushed the mouseover_misalignment branch from 6ca1962 to be67e7e Compare July 14, 2026 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: QuadMesh.get_cursor_data() returns incorrect value Reliably set the output image size

3 participants