FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Bug]: Changing GridSpec column/row ratios after axes creation does not update the figure layout · Issue #31971 · matplotlib/matplotlib · GitHub

Repository navigation

[Bug]: Changing GridSpec column/row ratios after axes creation does not update the figure layout #31971

Description

Bug summary

Updating a GridSpec's width or height ratios after axes have already been added to it has no visible effect — the figure layout silently stays unchanged, even though the internal state is updated correctly.

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure()
gs = gridspec.GridSpec(1, 3, figure=fig, width_ratios=[1, 2, 1])
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[0, 2])
fig.canvas.draw()

print("Before:", round(ax2.get_position().width, 4))

gs.set_width_ratios([1, 1, 1])
fig.canvas.draw()

print("After: ", round(ax2.get_position().width, 4))

fig.savefig('test.png')
plt.show()

Actual outcome

Before: 0.3419
After: 0.3419

Expected outcome

After the ratios are updated, all three axes should become equal width:

Before: 0.3419
After: 0.2279

Additional information

No response

Operating system

Ubuntu 24.04

Matplotlib Version

3.12.0.dev230+g1845e6647

Matplotlib Backend

tkagg

Python version

3.12.3

Jupyter version

No response

Installation

git checkout

Activity

timhoffm commented on Jun 30, 2026

Member

By default no layout mechanism is active, which means Axes are placed in their initial position and not moved afterwards.

You either have to set the suplot spec anew ax2.set_subplotspec(gs[0, 1]), or you can use a layout manager to control Axes positioning in the background (plt.figure(layout="constrained") or plt.figure(layout="tight")).

Overall, I don't think we can do anything here as long as there is no default layout mechanism in place.

pinned and unpinned a comment on Jun 30, 2026

Aayan-Ali-Hashim commented on Jun 30, 2026

Is this issue still open?

timhoffm commented on Jun 30, 2026

Member

IMHO there is nothing to do here. Just waiting for reply before closing.

Aayan-Ali-Hashim commented on Jul 1, 2026

IMHO there is nothing to do here. Just waiting for reply before closing.

Thanks Tim for letting me know.

jayaprajapatii commented on Jul 1, 2026

ContributorAuthor

Thanks @timhoffm That clears things up.

The issue isn't really about the layout engine. set_width_ratios() is a setter that updates internal state but has no visible effect, with no warning, no error, and no mention in the docstring. From a user's perspective, calling a setter and seeing nothing change is unexpected behavior regardless of the layout engine.

timhoffm commented on Jul 1, 2026

Member

From a user's perspective, calling a setter and seeing nothing change is unexpected behavior regardless of the layout engine.

Yes this is not ideal, but it's a setter on another object, so the expectation is not clear. fig.add_subplot(gs[0, 0]) could be regarded as "create the sublot at the given position" - This by itself does not indicate whether the Axes maintains a connection to the gridspec (It does technically, internally, but that is not used unless you do a repositioning through layout). So the logical default behavior is that the gridspec is just a parameter evalulated at creation time.

Technical: By default, there is no mechanism to react later changes. GridSpec/SubplotSpec are pure data objects. They don't know about Axes that use them, so can't push any changes. The only way to update is for Axes to re-evalutate their position. And that's the purpose of the layout mechanism.

So this is not ideal but bearable. You could write a minimal layout engine that updates the Axes from the specs, and make it the default. However, that's a specific solution for a very particular problem. On a larger, scale I'd rather discuss whether we can have a reasonable layout manager activated by default. We haven't done this for backward-compatibility (exact positioning reproducibility), but for most use cases having a default layout manger would be the sane and user-friendly choice nowadays.

rcomer commented on Jul 1, 2026

Member

a setter that updates internal state but has no visible effect, with no warning, no error, and no mention in the docstring

I guess there is no harm in updating the docstrings to clarify that these methods won't by themselves update existing axes.

jayaprajapatii commented on Jul 1, 2026

ContributorAuthor

I guess there is no harm in updating the docstrings to clarify that these methods won't by themselves update existing axes.

I agree with you @rcomer

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL