Title: Computing space needed for figure legend when using tight_layout · Issue #15257 · matplotlib/matplotlib · GitHub
Open Graph Title: Computing space needed for figure legend when using tight_layout · Issue #15257 · matplotlib/matplotlib
X Title: Computing space needed for figure legend when using tight_layout · Issue #15257 · matplotlib/matplotlib
Description: The use case it to place a figure legend below a grid of subplots, while also using tight_layout() to adjust the spacing between the subplots. This is possible using this code example, where LEGEND_HEIGHT must be set to the fraction of f...
Open Graph Description: The use case it to place a figure legend below a grid of subplots, while also using tight_layout() to adjust the spacing between the subplots. This is possible using this code example, where LEGEND...
X Description: The use case it to place a figure legend below a grid of subplots, while also using tight_layout() to adjust the spacing between the subplots. This is possible using this code example, where LEGEND...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/15257
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Computing space needed for figure legend when using tight_layout","articleBody":"The use case it to place a figure legend below a grid of subplots, while also using `tight_layout()` to adjust the spacing between the subplots. This is possible using this code example, where `LEGEND_HEIGHT` must be set to the fraction of figure height used by the figure:\r\n\r\n```python\r\nimport collections\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\nLEGEND_HEIGHT = 0.05\r\n\r\n# Create figure with a grid of plots and random data.\r\nfig, axes = plt.subplots(5, 5, figsize=(10, 8))\r\nfor ax in axes.flatten():\r\n ax.plot(np.cumsum(np.random.uniform(-1, 1, 100)), label='Line')\r\n\r\n# Collect legend labels from all plots.\r\nentries = collections.OrderedDict()\r\nfor ax in axes.flatten():\r\n for handle, label in zip(*ax.get_legend_handles_labels()):\r\n entries[label] = handle\r\n\r\n# Adjust spacing between plots and make space for legend.\r\nfig.tight_layout(rect=(0, LEGEND_HEIGHT, 1, 1), h_pad=0.5, w_pad=0.5)\r\n\r\n# Add legend below the grid of plots.\r\nlegend = fig.legend(\r\n entries.values(), entries.keys(),\r\n loc='upper center', bbox_to_anchor=(0.5, LEGEND_HEIGHT))\r\n\r\nfig.savefig('figure.png')\r\n```\r\n\r\n\r\n\r\nHowever, the number of subplots in the grid entries and entries in the legend frequently changes for me. Right now, this requires a lot of time for manually tuning `LEGEND_HEIGHT` to a suitable value. Does Matplotlib allow for an automatic solution here?","author":{"url":"https://github.com/danijar","@type":"Person","name":"danijar"},"datePublished":"2019-09-12T15:56:11.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/15257/matplotlib/issues/15257"}
| 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:a9a6a475-a2d3-b969-3083-9c16ac7a9e68 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EE42:323BC4:2549B25:32662ED:6A539A73 |
| html-safe-nonce | 5e7cccee2c83de4f1eaec188717af719ab5de1707c4a2bf1deba3e97d1b16199 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRTQyOjMyM0JDNDoyNTQ5QjI1OjMyNjYyRUQ6NkE1MzlBNzMiLCJ2aXNpdG9yX2lkIjoiNjU3MzY4MjAwOTM2MDgwMDM3MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 79d09e04d0867bc7fe35dd44834dbf8a22505247fc53c6812fb132ea8d30be05 |
| hovercard-subject-tag | issue:492894048 |
| 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/15257/issue_layout |
| twitter:image | https://opengraph.githubassets.com/574069a01ac0ff8b5de3cc52bcb2d82cf4b91ed1a697285fe4fb52da5ba787f1/matplotlib/matplotlib/issues/15257 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/574069a01ac0ff8b5de3cc52bcb2d82cf4b91ed1a697285fe4fb52da5ba787f1/matplotlib/matplotlib/issues/15257 |
| og:image:alt | The use case it to place a figure legend below a grid of subplots, while also using tight_layout() to adjust the spacing between the subplots. This is possible using this code example, where LEGEND... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | danijar |
| 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