Title: [ENH]: Adapt 2-dim histogram axis limits to content · Issue #26288 · matplotlib/matplotlib · GitHub
Open Graph Title: [ENH]: Adapt 2-dim histogram axis limits to content · Issue #26288 · matplotlib/matplotlib
X Title: [ENH]: Adapt 2-dim histogram axis limits to content · Issue #26288 · matplotlib/matplotlib
Description: Problem Plotting a 2-dim histogram using the hist2d function can leave a lot of excess space in the plot when using the argument cmin to exclude bins with insufficient numbers of data-points. (There's presumably a similar problem when us...
Open Graph Description: Problem Plotting a 2-dim histogram using the hist2d function can leave a lot of excess space in the plot when using the argument cmin to exclude bins with insufficient numbers of data-points. (Ther...
X Description: Problem Plotting a 2-dim histogram using the hist2d function can leave a lot of excess space in the plot when using the argument cmin to exclude bins with insufficient numbers of data-points. (Ther...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/26288
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[ENH]: Adapt 2-dim histogram axis limits to content","articleBody":"### Problem\n\nPlotting a 2-dim histogram using the `hist2d` function can leave a lot of excess space in the plot when using the argument `cmin` to exclude bins with insufficient numbers of data-points. (There's presumably a similar problem when using the argument `cmax`).\r\n\r\n %matplotlib inline\r\n import numpy as np\r\n import matplotlib.pyplot as plt\r\n\r\n # Generate random x/y values.\r\n rng = np.random.default_rng(1234)\r\n size = 100000\r\n x = rng.normal(scale=10, size=size)\r\n y = rng.normal(size=size)\r\n\r\n # This plot has properly adjusted axis-limits.\r\n plt.hist2d(x, y, bins=100, cmin=1);\r\n\r\n\r\n\r\n # This plot has a lot of excess space.\r\n plt.hist2d(x, y, bins=100, cmin=20);\r\n\r\n\r\n\n\n### Proposed solution\n\nThe following is a quick solution that seems to work. But it may be possible to make a better solution when integrating it into the `hist2d` function. It should be made as an optional argument such as `hist2d(adapt_lim=True, ...)` to adapt both x and y-axis, or specify a given axis such as `hist2d(adapt_lim='x', ...)` \r\n\r\nI don't really have time to add this to Matplotlib's code-base myself, and make sure everything is made according to your standards etc. So hopefully someone else can do it, if you like the feature. Thanks!\r\n\r\n # Plot 2-dim histogram and get bins and edges.\r\n h, xedges, yedges, _ = plt.hist2d(x, y, bins=100, cmin=20);\r\n\r\n # Boolean mask whether a bin is actually used in the 2-dim grid.\r\n mask = ~np.isnan(h)\r\n\r\n # Flattened boolean masks whether a bin is actually used for each axis.\r\n mask_flat_x = np.any(mask, axis=1)\r\n mask_flat_y = np.any(mask, axis=0)\r\n\r\n # Get x-axis min/max for bins that are actually used.\r\n x_min = xedges[:-1][mask_flat_x].min()\r\n x_max = xedges[1:][mask_flat_x].max()\r\n\r\n # Get y-axis min/max for bins that are actually used.\r\n y_min = yedges[:-1][mask_flat_y].min()\r\n y_max = yedges[1:][mask_flat_y].max()\r\n\r\n # Adjust x-axis limits to have a little padding.\r\n x_pad = 0.01 * (x_max - x_min)\r\n plt.xlim(x_min - x_pad, x_max + x_pad)\r\n\r\n # Adjust y-axis limits to have a little padding.\r\n y_pad = 0.01 * (y_max - y_min)\r\n plt.ylim(y_min - y_pad, y_max + y_pad)\r\n\r\n plt.show();\r\n\r\n\r\n","author":{"url":"https://github.com/Hvass-Labs","@type":"Person","name":"Hvass-Labs"},"datePublished":"2023-07-11T15:47:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/26288/matplotlib/issues/26288"}
| 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:9545f4fc-88fd-626d-cbf3-586db571fe55 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A9BC:159AA0:6094D2:871BDB:6A5104F4 |
| html-safe-nonce | ff1227e1993d6658164cba843327f7453e4b943382bfe3ef788a9fc1367b8d91 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOUJDOjE1OUFBMDo2MDk0RDI6ODcxQkRCOjZBNTEwNEY0IiwidmlzaXRvcl9pZCI6Ijg4MzU3MzUyNDQ1ODk2NDIxMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6c69bbe5c0b08b91938b436637b0faf9b7a7861b20f5ce173c8e1d0ea5e55c26 |
| hovercard-subject-tag | issue:1799236229 |
| 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/26288/issue_layout |
| twitter:image | https://opengraph.githubassets.com/877ac17d14933761a4918b7fa1069bcfd8dbe04b456f70789ec4245b02b3c95a/matplotlib/matplotlib/issues/26288 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/877ac17d14933761a4918b7fa1069bcfd8dbe04b456f70789ec4245b02b3c95a/matplotlib/matplotlib/issues/26288 |
| og:image:alt | Problem Plotting a 2-dim histogram using the hist2d function can leave a lot of excess space in the plot when using the argument cmin to exclude bins with insufficient numbers of data-points. (Ther... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Hvass-Labs |
| hostname | github.com |
| expected-hostname | github.com |
| None | 38906a4da4e3e9fd88dfeacee232f5932c0c5f7495de3b4efc53ddfac7173d12 |
| 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 | 90441ea9b98e7491aa6ebcd135fccce4e2396f48 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width