Title: Adding colorbar without affecting the plotting area size. · Issue #15010 · matplotlib/matplotlib · GitHub
Open Graph Title: Adding colorbar without affecting the plotting area size. · Issue #15010 · matplotlib/matplotlib
X Title: Adding colorbar without affecting the plotting area size. · Issue #15010 · matplotlib/matplotlib
Description: I would like to have a .mplstyle that by default produces square plots, per discussion started in #15001 and I still think "square" plots would be a reasonable thing to be able to set as default. tl;dr Want square plots of the same size ...
Open Graph Description: I would like to have a .mplstyle that by default produces square plots, per discussion started in #15001 and I still think "square" plots would be a reasonable thing to be able to set as default. t...
X Description: I would like to have a .mplstyle that by default produces square plots, per discussion started in #15001 and I still think "square" plots would be a reasonable thing to be able to set as ...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/15010
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Adding colorbar without affecting the plotting area size.","articleBody":"I would like to have a `.mplstyle` that by default produces square plots, per discussion started in #15001 and I still think \"square\" plots would be a reasonable thing to be able to set as default. \r\n\r\n**tl;dr Want square plots of the same size with or without colorbar. What should change is the xdim of the figure.**\r\n\r\nAnyway, for a single plot this is easy setting `figsize` either live or in the style. This doesn't let me specify the box size other than manually, but I can live with that\r\n```\r\nx = np.linspace(0,1,51)\r\ny = np.random.uniform(0,5,51)\r\nz = np.random.normal(0,1,51)\r\n\r\nfig = plt.figure(figsize=(5,5))\r\nax = fig.add_subplot()\r\nax.scatter(x, y, c = z);\r\n```\r\n\r\n\r\nIt gets trickier if I want to add a colorbar as well, but I would like the main box size to not change. The simplest method as far as I can tell is with `fig.colorbar()`\r\n```\r\nfig = plt.figure(figsize=(5,5))\r\nax = fig.add_subplot()\r\nsc = ax.scatter(x, y, c = z);\r\nfig.colorbar(sc, ax=ax);\r\n```\r\nHowever, this modifies the main box to fit within figure and changes the aspect.\r\n\r\n\r\nThe other option is to use `make_axes_locatable` Which if I understand correctly better splits the current axes and some reason this results in a less screwed up aspect than the above, but still changes it.\r\n```\r\nfrom mpl_toolkits.axes_grid1 import make_axes_locatable\r\nfig = plt.figure(figsize=(5,5))\r\nax = fig.add_subplot()\r\ndivider = make_axes_locatable(ax)\r\ncax = divider.append_axes('right', size=\"7%\", pad=0.2,)\r\nsc = ax.scatter(x, y, c = z);\r\ncbar = fig.colorbar(sc, cax=cax);\r\n```\r\n\r\n\r\nCurrent best solution I found is resizing figure afterwards based on a set axes size with @ImportanceOfBeingErnest 's answer https://stackoverflow.com/a/44971177. However, this is sub optimal because based on ticks/labels etc it doesn't always produce the same size output figure, which can be a problem when formatting a tex document later for example. \r\n\r\nOne solutions would be to have custom function(s) presetting the sizes manually like `make_rgb_axes` in https://matplotlib.org/3.1.1/gallery/axes_grid1/demo_axes_rgb.html which would get rid of the trial and error of setting the fig/ax size until it's just right. The obvious drawback of that is someone then wants a different size cbar or different padding it will screw up the layout and the fact that it needs to be packages somewhere else, which makes it more complicated for user to look up syntax/problems.\r\n","author":{"url":"https://github.com/andrzejnovak","@type":"Person","name":"andrzejnovak"},"datePublished":"2019-08-08T10:45:14.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":21},"url":"https://github.com/15010/matplotlib/issues/15010"}
| 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:13b92e7f-cbda-c760-2b99-ee774436d2fb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D0A4:32BAC1:9E8F2C:D465D9:6A52B5E3 |
| html-safe-nonce | 92557e8793386b0a26f8ebd922380662bbf84bb72715c2f4a2093be449806186 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMEE0OjMyQkFDMTo5RThGMkM6RDQ2NUQ5OjZBNTJCNUUzIiwidmlzaXRvcl9pZCI6IjM0MDgzNzAzNTQzNTUzNTMwNjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2232ec2f9e6dae9541af40841d19407c5f1b3520e4280670db47547e23b6a5ae |
| hovercard-subject-tag | issue:478392111 |
| 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/15010/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bcec47eb1df4d8ec5513239ad160fee31225c97b574538a9f1e35c647aa2fd5f/matplotlib/matplotlib/issues/15010 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bcec47eb1df4d8ec5513239ad160fee31225c97b574538a9f1e35c647aa2fd5f/matplotlib/matplotlib/issues/15010 |
| og:image:alt | I would like to have a .mplstyle that by default produces square plots, per discussion started in #15001 and I still think "square" plots would be a reasonable thing to be able to set as default. t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | andrzejnovak |
| 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