Title: [Bug]: Different behavior of tight_layout and set_tight_layout with matplotlib 3.5.0 on macOS · Issue #21673 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: Different behavior of tight_layout and set_tight_layout with matplotlib 3.5.0 on macOS · Issue #21673 · matplotlib/matplotlib
X Title: [Bug]: Different behavior of tight_layout and set_tight_layout with matplotlib 3.5.0 on macOS · Issue #21673 · matplotlib/matplotlib
Description: Bug summary Since matplotlib 3.5.0, I see a slightly different result when using tight_layout() or set_tight_layout(True) with the macOS backend. These differences do not appear with matplotlib 3.4.3, or in a Debian container. Code for r...
Open Graph Description: Bug summary Since matplotlib 3.5.0, I see a slightly different result when using tight_layout() or set_tight_layout(True) with the macOS backend. These differences do not appear with matplotlib 3.4...
X Description: Bug summary Since matplotlib 3.5.0, I see a slightly different result when using tight_layout() or set_tight_layout(True) with the macOS backend. These differences do not appear with matplotlib 3.4...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/21673
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: Different behavior of tight_layout and set_tight_layout with matplotlib 3.5.0 on macOS","articleBody":"### Bug summary\n\nSince `matplotlib` 3.5.0, I see a slightly different result when using `tight_layout()` or `set_tight_layout(True)` with the macOS backend. These differences do not appear with `matplotlib` 3.4.3, or in a Debian container.\n\n### Code for reproduction\n\n```python\nimport matplotlib.pyplot as plt\r\nfrom matplotlib.testing.compare import compare_images\r\n\r\n\r\nfig1, ax1 = plt.subplots()\r\nfig1.set_tight_layout(True)\r\nfig1.savefig(\"1.png\")\r\n\r\nfig2, ax2 = plt.subplots()\r\nfig2.tight_layout()\r\nfig2.savefig(\"2.png\")\r\n\r\nassert compare_images(\"1.png\", \"2.png\", 0) is None\n```\n\n\n### Actual outcome\n\nThe comparison fails, and the figures slightly differ on macOS:\r\n```python\r\nTraceback (most recent call last):\r\n File \"test.py\", line 13, in \u003cmodule\u003e\r\n assert compare_images(\"1.png\", \"2.png\", 0) is None\r\nAssertionError\r\n```\r\n\r\nThe difference is rather small: the rightmost x-axis tick label slightly moves.\r\n\r\nOn Debian the assertion succeeds, and when downgrading to `matplotlib` 3.4.3 it also succeeds on macOS.\n\n### Expected outcome\n\nAccording to the [tight layout guide](https://matplotlib.org/stable/tutorials/intermediate/tight_layout_guide.html):\r\n\r\n\u003e Note that `matplotlib.pyplot.tight_layout()` will only adjust the subplot params when it is called. In order to perform this adjustment each time the figure is redrawn, you can call `fig.set_tight_layout(True)`, or, equivalently, set `rcParams[\"figure.autolayout\"]` (default: `False`) to `True`.\r\n\r\nI expect both figures to be equivalent, as there is nothing else happening after the `tight_layout()` / `set_tight_layout(True)` call and before saving.\n\n### Additional information\n\nI can only reproduce the behavior on macOS with `matplotlib` 3.5.0. Earlier versions produce consistent figures, and a Debian container with `python:3.9-slim` also produces consistent figures with `matplotlib` 3.4.3.\r\n\r\nI came across this while trying to understand differences in figure layout observed between CI running Ubuntu and a local macOS setup, which appeared with `matplotlib` 3.5.0 when using `tight_layout()`. These differences disappear with `set_tight_layout(True)`, and I also see consistent behavior with `constrained_layout`.\r\n\r\nWhile the observed difference in the example above is very small, the differences become slightly larger with more complex setups. [This gist](https://gist.github.com/alexander-held/8f429e1edde677be54045befec8c2f7b) shows another figure comparison where the differences are a bit more noticeable (though still small, and hard to see unless flipping back and forth between the images).\r\n\r\nWhen switching the backend to TkAgg (`matplotlib.use(\"TkAgg\")`), the discrepancy also disappears.\n\n### Operating system\n\nmacOS 11.5.2\n\n### Matplotlib Version\n\n3.5.0\n\n### Matplotlib Backend\n\nMacOSX\n\n### Python version\n\nPython 3.8.10\n\n### Jupyter version\n\n6.4.0\n\n### Installation\n\npip","author":{"url":"https://github.com/alexander-held","@type":"Person","name":"alexander-held"},"datePublished":"2021-11-18T18:12:25.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/21673/matplotlib/issues/21673"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e8ca4cd1-3d1a-cae3-affc-0ee6418d57e5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A716:1D5589:1A3C38F:22EB3C8:6A52CB49 |
| html-safe-nonce | fc4788bf03c1edaf2930efbe0ed8e3f6bfd5dc94c40e93b56e5e49a2b9fa624e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzE2OjFENTU4OToxQTNDMzhGOjIyRUIzQzg6NkE1MkNCNDkiLCJ2aXNpdG9yX2lkIjoiOTA3MDYwMjY4OTAyOTc4NjQ0MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4088d6a908510ce5e0cf9c49c1e920610600d7e309540b1319676be58090a100 |
| hovercard-subject-tag | issue:1057653723 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/21673/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f6c02893f6235a855157273f95ce1b6aa94d1b2f065bcbdf4255964c6bd08223/matplotlib/matplotlib/issues/21673 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f6c02893f6235a855157273f95ce1b6aa94d1b2f065bcbdf4255964c6bd08223/matplotlib/matplotlib/issues/21673 |
| og:image:alt | Bug summary Since matplotlib 3.5.0, I see a slightly different result when using tight_layout() or set_tight_layout(True) with the macOS backend. These differences do not appear with matplotlib 3.4... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | alexander-held |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width