Title: [Bug]: colorbar doesn't register inset_axis as cax · Issue #27763 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: colorbar doesn't register inset_axis as cax · Issue #27763 · matplotlib/matplotlib
X Title: [Bug]: colorbar doesn't register inset_axis as cax · Issue #27763 · matplotlib/matplotlib
Description: Bug summary When constructing inset axes using the tutorial on this page the gradient of the colorbar does not appear in the constructed inset axis but somewhere else on the figure (location seems to depend on figure size) once saved to ...
Open Graph Description: Bug summary When constructing inset axes using the tutorial on this page the gradient of the colorbar does not appear in the constructed inset axis but somewhere else on the figure (location seems ...
X Description: Bug summary When constructing inset axes using the tutorial on this page the gradient of the colorbar does not appear in the constructed inset axis but somewhere else on the figure (location seems ...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/27763
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: colorbar doesn't register inset_axis as cax ","articleBody":"### Bug summary\n\nWhen constructing inset axes using the tutorial on [this page](https://matplotlib.org/stable/gallery/axes_grid1/demo_colorbar_with_inset_locator.html) the gradient of the colorbar does not appear in the constructed inset axis but somewhere else on the figure (location seems to depend on figure size) once saved to a PDF with multiple other figures if bbox_inches='tight'. Similar to #27143 and #25176.\n\n### Code for reproduction\n\n```python\nfrom mpl_toolkits.axes_grid1.inset_locator import inset_axes\r\nimport matplotlib.pylab as plt\r\nfrom matplotlib.backends.backend_pdf import PdfPages\r\n\r\n#make the first figure\r\nfig, axes = plt.subplots(1, 1, facecolor = 'white', figsize = (20, 5), constrained_layout = True)\r\n\r\nx = random.randint(100, size=(100, 2048))\r\nc = axes.imshow(x, origin = 'lower', cmap = 'Reds', vmin = 0, vmax = 100, aspect = \"auto\")\r\naxins1 = inset_axes(axes,\r\n width=\"10%\",\r\n height=\"30%\",\r\n loc=\"upper left\",\r\n bbox_to_anchor = (0.045, 0., 1, 1),\r\n bbox_transform = axes.transAxes\r\n )\r\naxins1.xaxis.set_ticks_position(\"bottom\")\r\nfig.colorbar(c, cax=axins1, orientation=\"horizontal\")\r\naxes.set_yticklabels([])\r\n\r\nfig.suptitle(\"Title\")\r\nplt.figtext(-0.02, 0.5, 'Random', c = 'k', rotation = 90, ma = 'center', fontsize = medium)\r\n\r\n#make figure 2\r\nfig2, axes = plt.subplots(1, 1, facecolor = 'white', figsize = (20, 5), constrained_layout = True)\r\n\r\nx = random.randint(100, size=(100, 2048))\r\nc = axes.imshow(x, origin = 'lower', cmap = 'Reds', vmin = 0, vmax = 100, aspect = \"auto\")\r\naxins1 = inset_axes(axes,\r\n width=\"10%\",\r\n height=\"30%\",\r\n loc=\"upper left\",\r\n bbox_to_anchor = (0.045, 0., 1, 1),\r\n bbox_transform = axes.transAxes\r\n )\r\naxins1.xaxis.set_ticks_position(\"bottom\")\r\nfig2.colorbar(c, cax=axins1, orientation=\"horizontal\")\r\naxes.set_yticklabels([])\r\n\r\nfig2.suptitle(\"Title\")\r\nplt.figtext(-0.02, 0.5, 'Random', c = 'k', rotation = 90, ma = 'center', fontsize = medium)\r\n\r\n#save figs to PDF\r\nfigs = [fig, fig2]\r\nwith PdfPages('test.pdf') as pdf:\r\n for fig in figs:\r\n pdf.savefig(fig, bbox_inches='tight')\n```\n\n\n### Actual outcome\n\nThe gradient for the colorbar appears outside of the inset axes on each of the plots\r\n[test_small.pdf](https://github.com/matplotlib/matplotlib/files/14215554/test_small.pdf)\r\n\n\n### Expected outcome\n\nThe result for this particular example when bbox_inches='tight' is not called in savefig(). Note: when running in jupyter lab this is always the outcome--only when saving the figure does the gradient jump out of the inset axis.\r\n[test_loose.pdf](https://github.com/matplotlib/matplotlib/files/14215555/test_loose.pdf)\r\n\n\n### Additional information\n\nI tried the same suggestion as in #27143 and used `axins1 = axes.inset_axes([0.05, 0.7, 0.1, 0.2], transform = axes.transAxes)` in my own code which seemed to fix this. Unsure why the example constructor for inset_axes [here](https://matplotlib.org/stable/gallery/axes_grid1/demo_colorbar_with_inset_locator.html) doesn't work in this case.\n\n### Operating system\n\nMac OS 13.6.3 (Ventura) Intel\n\n### Matplotlib Version\n\n3.8.0\n\n### Matplotlib Backend\n\nInstalled osx event loop hook. MacOSX\n\n### Python version\n\nPython 3.9.18\n\n### Jupyter version\n\nJupyter Lab 3.6.3\n\n### Installation\n\nconda","author":{"url":"https://github.com/ericasaw","@type":"Person","name":"ericasaw"},"datePublished":"2024-02-08T23:28:26.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":12},"url":"https://github.com/27763/matplotlib/issues/27763"}
| 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:07aebfaf-f96f-2a77-2f93-20fe91e1834b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EC64:CF2E5:A2CA32:DEC2DA:6A520438 |
| html-safe-nonce | 64c2a9b235640669116e3de5b8e3cfc9d422227183272c90c3e6ecaf414a8ffa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQzY0OkNGMkU1OkEyQ0EzMjpERUMyREE6NkE1MjA0MzgiLCJ2aXNpdG9yX2lkIjoiOTEyMDM5OTk2MTQ5MzczNDQ1NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fdf13913e404ac81ec083591ba01a1ec6242f75b7d14852448ec2fa2fbc3a3f8 |
| hovercard-subject-tag | issue:2126229744 |
| 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/27763/issue_layout |
| twitter:image | https://opengraph.githubassets.com/baa3726979d41855a9779c5552fbb84feb1d85904c18c7efc29527f605bcfb24/matplotlib/matplotlib/issues/27763 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/baa3726979d41855a9779c5552fbb84feb1d85904c18c7efc29527f605bcfb24/matplotlib/matplotlib/issues/27763 |
| og:image:alt | Bug summary When constructing inset axes using the tutorial on this page the gradient of the colorbar does not appear in the constructed inset axis but somewhere else on the figure (location seems ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ericasaw |
| 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 | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width