Skip to content

WIP: Fix floating-point precision affecting GridSpec bottom and right#31703

Draft
ayshih wants to merge 2 commits into
matplotlib:mainfrom
ayshih:gridspec_bottom_right
Draft

WIP: Fix floating-point precision affecting GridSpec bottom and right#31703
ayshih wants to merge 2 commits into
matplotlib:mainfrom
ayshih:gridspec_bottom_right

Conversation

@ayshih
Copy link
Copy Markdown
Contributor

@ayshih ayshih commented May 18, 2026

PR summary

Currently, due to floating-point precision, GridSpec has the quirk that the returned bottom and right might not exactly match the parameter-specified bottom and right.

For example, for a single subplot and default parameters, the axes bottom should be 0.11 and the axes top should 0.88, but the axes bottom ends up being off by one floating-point tick:

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> ax.get_position()
Bbox([[0.125, 0.10999999999999999], [0.9, 0.88]])

That's because 0.88 - (0.88 - 0.11) gets computed as 0.10999999999999999 and not 0.11.

This PR forces the GridSpec-returned bottom and right to be the parameter-specified bottom and right:

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> ax.get_position()
Bbox([[0.125, 0.11], [0.9, 0.88]])

AI Disclosure

No AI was used

PR checklist

@github-actions github-actions Bot added the topic: geometry manager LayoutEngine, Constrained layout, Tight layout label May 18, 2026
fig_tops, fig_bottoms = (top - cell_hs).reshape((-1, 2)).T
fig_lefts, fig_rights = (left + cell_ws).reshape((-1, 2)).T

# avoid floating-point precision where A + (B - A) does not equal B
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe worth a comment that this is the same strategy (brutally forcing the value of the last point) as the one employed by linspace, from a quick skim at their implementation.

In general the patch looks fine to me.

@tacaswell
Copy link
Copy Markdown
Member

h5py/h5py#1115 this is the h5py issue I mentioned on the call (I think I crossed it with a different issue with non-standard floats).

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

Labels

topic: geometry manager LayoutEngine, Constrained layout, Tight layout

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants