Title: legend is not present in the generated image if I use 'tight' for bbox_inches · Issue #10194 · matplotlib/matplotlib · GitHub
Open Graph Title: legend is not present in the generated image if I use 'tight' for bbox_inches · Issue #10194 · matplotlib/matplotlib
X Title: legend is not present in the generated image if I use 'tight' for bbox_inches · Issue #10194 · matplotlib/matplotlib
Description: summary of the problem I tried to put legend outside of the axes. If I use fig.legend() method to produce legend and use bbox_inches='tight' in savefig() method. The legend is not present in the produced image (using the argument bbox_ex...
Open Graph Description: summary of the problem I tried to put legend outside of the axes. If I use fig.legend() method to produce legend and use bbox_inches='tight' in savefig() method. The legend is not present in the pr...
X Description: summary of the problem I tried to put legend outside of the axes. If I use fig.legend() method to produce legend and use bbox_inches='tight' in savefig() method. The legend is not present i...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/10194
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"legend is not present in the generated image if I use 'tight' for bbox_inches","articleBody":"## summary of the problem\r\nI tried to put legend outside of the axes. If I use `fig.legend()` method to produce legend and use `bbox_inches='tight'` in `savefig()` method. The legend is not present in the produced image (using the argument `bbox_extra_artists` also does not work). If I use `ax.legend()` instead, the legend is present in the saved image.\r\n\r\n**Code for reproduction**\r\n```python\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nimport matplotlib as mpl\r\n\r\nx = np.arange(-5, 5, 0.1)\r\ny1 = np.sin(x)\r\ny2 = np.cos(x)\r\n\r\ny3 = np.sin(2*x)\r\ny4 = np.cos(2*x)\r\n\r\nfig, (ax1, ax2) = plt.subplots(ncols=1, nrows=2, figsize=(10, 6))\r\n\r\nax1.plot(x, y1, label='sin')\r\nax1.plot(x, y2, label='cos')\r\n\r\nax2.plot(x, y3, label='sin')\r\nax2.plot(x, y4, label='cos')\r\n\r\nhandles, labels = ax1.get_legend_handles_labels()\r\n# following code does not work\r\nlegend = fig.legend(handles, labels, loc='lower left', ncol=2, frameon=False,\r\n bbox_to_anchor=(0.12, 0.88))\r\nplt.savefig('test.jpg', bbox_extra_artists=[legend,], bbox_inches='tight')\r\n\r\n# following code works\r\n# legend = ax1.legend(handles, labels, ncol=2, frameon=False,\r\n# loc='lower left', bbox_to_anchor=(-0.01, 1.2))\r\n# plt.savefig('test.jpg', bbox_inches='tight')\r\nplt.show()\r\n```\r\n## outcome\r\nThe saved images are different if I use different legend method.\r\n### using `fig.legend` method\r\n\r\n\r\n### using `ax.legend` method\r\n\r\n\r\n\r\n**Matplotlib version**\r\n * Operating system: Windows 8.1 and Linux (CentOS 7) (This problem is maybe platform independent)\r\n * Matplotlib version: 2.0.2(Linux), 2.0.0 (Windows)\r\n * Matplotlib backend: Qt5Agg\r\n * Python version: 3.5.2 (Windows), 3.6.2 (Linux)\r\n\r\nThe matplotlib and python are all distributed with Anaconda.\r\n\r\n\r\n## quesiton\r\nIs this a bug or something I do not know. Maybe there is difference between `fig.legend` and `ax.legend` method?","author":{"url":"https://github.com/jdhao","@type":"Person","name":"jdhao"},"datePublished":"2018-01-08T03:49:49.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":16},"url":"https://github.com/10194/matplotlib/issues/10194"}
| 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:18bbfa18-90e2-242d-4e00-774b80da9ef5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CAE2:152AB4:785097:A1722D:6A5307D3 |
| html-safe-nonce | 71bc53813eea7391c043875679e6565e68cc084dadfeeaa81db5851bcdcf43c4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUUyOjE1MkFCNDo3ODUwOTc6QTE3MjJEOjZBNTMwN0QzIiwidmlzaXRvcl9pZCI6IjM4NjQxODkzNDUwNzk2OTEyMTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 354027eb491446742e60f0c48944d4fca4a5849534ee46992113d62a9cc3fb62 |
| hovercard-subject-tag | issue:286635924 |
| 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/10194/issue_layout |
| twitter:image | https://opengraph.githubassets.com/27f75037dab040ad93bfe0b5db42898067c6afe4bf60295e92bbf58a6b575d0c/matplotlib/matplotlib/issues/10194 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/27f75037dab040ad93bfe0b5db42898067c6afe4bf60295e92bbf58a6b575d0c/matplotlib/matplotlib/issues/10194 |
| og:image:alt | summary of the problem I tried to put legend outside of the axes. If I use fig.legend() method to produce legend and use bbox_inches='tight' in savefig() method. The legend is not present in the pr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jdhao |
| 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