Title: [ENH]: API discussion for grouped bar charts · Issue #24313 · matplotlib/matplotlib · GitHub
Open Graph Title: [ENH]: API discussion for grouped bar charts · Issue #24313 · matplotlib/matplotlib
X Title: [ENH]: API discussion for grouped bar charts · Issue #24313 · matplotlib/matplotlib
Description: Problem Currently, if one wants to draw multiple categories of bars side-by-side as in https://matplotlib.org/devdocs/gallery/lines_bars_and_markers/barchart.html, one has to calculate the bar positions manually. This is really a nuissan...
Open Graph Description: Problem Currently, if one wants to draw multiple categories of bars side-by-side as in https://matplotlib.org/devdocs/gallery/lines_bars_and_markers/barchart.html, one has to calculate the bar posi...
X Description: Problem Currently, if one wants to draw multiple categories of bars side-by-side as in https://matplotlib.org/devdocs/gallery/lines_bars_and_markers/barchart.html, one has to calculate the bar posi...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/24313
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[ENH]: API discussion for grouped bar charts","articleBody":"### Problem\r\n\r\nCurrently, if one wants to draw multiple categories of bars side-by-side as in https://matplotlib.org/devdocs/gallery/lines_bars_and_markers/barchart.html, one has to calculate the bar positions manually. This is really a nuissance and too low level for any user-facing interface. Actually, when asked I recommend to use pandas plotting functions for that if possible, which is really embarrasing.\r\n\r\nThere have been stalled attempts to do this (issure 10610, PR #11048). Additionally, this often gets related to a function for stacked bars #14086.\r\n\r\n\r\n\r\n### Proposed solution\r\n\r\nI'd like to pick up this topic and come up with a reasonable API. This topic is really complex and one can easily get lost in various details. For the design procedure, I take a bottom-up appoach by starting with a basic function `grouped_bar()` that does only expose the minimal functionality to get the plot done. I then intended to additional parameters one by one as they fit in. - So don't be concerned that the first proposal here is quite basic.\r\n\r\n**Terminology** I'll use *label* for the x-values, i.e. 'G1' .. 'G5' in above example, and *group* for the categories, i.e. 'Tea'/'Coffee'.\r\n\r\n---\r\n\r\n### For now: Only vertical orientation\r\nI'll limit the discussion here to 'vertical'. We can have a separate discussion whether we want to add an *orientation* parameter or make a `grouped_barh`. Both are technically easy and only an API design decision that's orthogonal to the rest of the API.\r\n\r\n### For now: Only grouped layout, no stacked layout\r\nTo keep things simple, I'll limit myself to grouped for now, because:\r\n- Stacked is somewhat simpler than grouped as you only need to insert the *bottom* values in multiple calls. For two bars it's just the heights of the firsts, and a cumsum for more than two.\r\n- We may build a separate `stacked_bar()` function, if the first bullet point is considered too cumbersome.\r\n- It's conceivable to unite both in one function as poposed in #11048 and realized in `DataFrame.plot.bar`, but that needs careful additional consideration.\r\n\r\neither way let's defer stacked bars to later.\r\n\r\n### Minimal API\r\n\r\nWe want to be able to rewrite https://matplotlib.org/devdocs/gallery/lines_bars_and_markers/barchart.html as\r\n\r\n```\r\ngrouped_bar(labels, [tea_means, coffee_means], group_labels=['Tea', 'Coffee']) \r\n```\r\n\r\nThus, the minimal API is:\r\n\r\n```\r\ndef grouped_bar(x, heights, *, group_labels=None):\r\n \"\"\"\r\n Parameters\r\n -----------\r\n x : array-like of str\r\n The labels.\r\n heights : list of array-like:\r\n An iterable of array-like: The iteration runs over the groups.\r\n Each individual array-like is the list of label values for that group.\r\n group_labels : array-like of str, optional\r\n The labels of the data groups.\r\n \"\"\"\r\n```\r\n\r\n---\r\n\r\nI'll soon expand on the minimal API, answering a lot of questions from https://github.com/matplotlib/matplotlib/pull/11048#issuecomment-381812675. But before that, please speak up in case you have fundamental concerns with adding such functionality at all or with the bottom-up design approach. OTOH if you think this is worth pursuing, please give a :+1:.","author":{"url":"https://github.com/timhoffm","@type":"Person","name":"timhoffm"},"datePublished":"2022-10-30T22:21:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":27},"url":"https://github.com/24313/matplotlib/issues/24313"}
| 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:08ba58d7-4563-35a8-4acc-94bf16617463 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DA94:E66FE:2901A5:376EE0:6A522891 |
| html-safe-nonce | f9e9accfe667153352ba888518ffc70532eda3cc70f2d20726e140819091197e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQTk0OkU2NkZFOjI5MDFBNTozNzZFRTA6NkE1MjI4OTEiLCJ2aXNpdG9yX2lkIjoiMjQ0NTQ4MDMyNjMwMDA1MTYwMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 817f119a7d4cb2e75dff1f8f3e70104022a0d70526c8ae911559598706139e56 |
| hovercard-subject-tag | issue:1429037866 |
| 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/24313/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1a33f8842a60caaa566e3951b6a8e001e38da39b203b5222c8af687670b2eeae/matplotlib/matplotlib/issues/24313 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1a33f8842a60caaa566e3951b6a8e001e38da39b203b5222c8af687670b2eeae/matplotlib/matplotlib/issues/24313 |
| og:image:alt | Problem Currently, if one wants to draw multiple categories of bars side-by-side as in https://matplotlib.org/devdocs/gallery/lines_bars_and_markers/barchart.html, one has to calculate the bar posi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | timhoffm |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width