Title: ENH have ax.get_tightbbox have a bbox around all artists attached to axes. by jklymak · Pull Request #10682 · matplotlib/matplotlib · GitHub
Open Graph Title: ENH have ax.get_tightbbox have a bbox around all artists attached to axes. by jklymak · Pull Request #10682 · matplotlib/matplotlib
X Title: ENH have ax.get_tightbbox have a bbox around all artists attached to axes. by jklymak · Pull Request #10682 · matplotlib/matplotlib
Description: PR Summary This PR takes the code that was in backend_bases.py in savefig for determining a figure bounding box, and puts it in ax.get_tightbbox and fig.get_tightbbox, via a refactoring into Artists.get_tightbbox. Supercedes #10678 and extends already merged #9164 In previous PRs I was piecemeal adding new types of artists, but noted that fig.savefig(bbox_inches='tight') was actually doing the correct thing and putting the bbox around all the artists. To get the old behaviour back, a new kwarg has been added: ax.get_tightbbox(bbox_extra_artists=[]) So now the following code import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(5,2)) ax.set_xlim([0, 1]) ax.text(1, 0.5, 'This is a long overflow') print('before', ax.get_tightbbox(fig.canvas.get_renderer(),bbox_extra_artists=[])) print('now ', ax.get_tightbbox(fig.canvas.get_renderer())) returns: before Bbox(x0=61.2, y0=-3.4, x1=922.125, y1=363.0) now Bbox(x0=61.2, y0=-3.4, x1=1210.25, y1=363.0) This is a breaking change, in that anyone who was expecting the bbox to not include artists, but just the axes axis elements and labels, will now get a different behaviour. They can get the old behaviour as above (specifying the empty bbox_extra_artists kwarg. OTOH, willing be told this is too big an API change. (it of course still needs an API change note, and likely a test or two somewhere). UPDATE 28 April: This adds the artist property artist.set/get_in_layout (as discussed w/ @efiring et al on the weekly call) which specifies if an artist should be included in the tight_bbox calculation. This allows toggling artists in the tight layout or constrained layout calculations. So, now we can also do: fig, ax = plt.subplots(figsize=(5,2)) ax.set_xlim([0, 1]) t = ax.text(1, 0.5, 'This is a long overflow') t.inbbox = True # default print('inbbox true', ax.get_tightbbox(fig.canvas.get_renderer())) t.set_in_layout(False) print('inbbox false ', ax.get_tightbbox(fig.canvas.get_renderer())) t.set_in_layout(True) print('inbbox true ', ax.get_tightbbox(fig.canvas.get_renderer())) and get in_layout true Bbox(x0=61.18055555555556, y0=-3.4444444444444535, x1=1210.25, y1=363.0) in_layout false Bbox(x0=61.18055555555556, y0=-3.4444444444444535, x1=922.125, y1=363.0) in_layout true Bbox(x0=61.18055555555556, y0=-3.4444444444444535, x1=1210.25, y1=363.0) Note that this also works for legends, which often get put outside axes and hence may not want to be part of the tight_layout machinery. Discussion: Should artists that did not get included in the old tight_layout be set to False by default? I'm somewhat against this because it means users have to guess or query what state the attribute is in... PR Checklist Has Pytest style unit tests Code is PEP 8 compliant Documentation is sphinx and numpydoc compliant Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way
Open Graph Description: PR Summary This PR takes the code that was in backend_bases.py in savefig for determining a figure bounding box, and puts it in ax.get_tightbbox and fig.get_tightbbox, via a refactoring into Artist...
X Description: PR Summary This PR takes the code that was in backend_bases.py in savefig for determining a figure bounding box, and puts it in ax.get_tightbbox and fig.get_tightbbox, via a refactoring into Artist...
Opengraph URL: https://github.com/matplotlib/matplotlib/pull/10682
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:d4673efc-2cb3-c830-a0f7-f8160c1d3b47 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D000:16035A:3F80356:5846AF5:6A54D6E0 |
| html-safe-nonce | e6d7645a55d21ec0412920e29f29539f102189f50a78f9b2bfaf702506576eed |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMDAwOjE2MDM1QTozRjgwMzU2OjU4NDZBRjU6NkE1NEQ2RTAiLCJ2aXNpdG9yX2lkIjoiNDAyMzE4MzU5MjY1NDE2NTcyOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7bd99e00cc0c00cea6f10fdadb97b1b66af96533759208f0712b5cbf3c22e7f7 |
| hovercard-subject-tag | pull_request:172776563 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/matplotlib/matplotlib/pull/10682/files |
| twitter:image | https://avatars.githubusercontent.com/u/1562854?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/1562854?s=400&v=4 |
| og:image:alt | PR Summary This PR takes the code that was in backend_bases.py in savefig for determining a figure bounding box, and puts it in ax.get_tightbbox and fig.get_tightbbox, via a refactoring into Artist... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | a556215b071af6609619e2bbe6f00bdfbf0812ad723b1ae6c301858a7a829f54 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 840b5d20d5a883c23cf56f5240b2c343d6cc0867 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width