Title: [Bug]: Axes.grouped_bar() with non-string orientation (e.g., NumPy array) raises ambiguous truth-value error instead of clean ValueError · Issue #30706 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: Axes.grouped_bar() with non-string orientation (e.g., NumPy array) raises ambiguous truth-value error instead of clean ValueError · Issue #30706 · matplotlib/matplotlib
X Title: [Bug]: Axes.grouped_bar() with non-string orientation (e.g., NumPy array) raises ambiguous truth-value error instead of clean ValueError · Issue #30706 · matplotlib/matplotlib
Description: Bug summary Passing a non-string value (like a NumPy array) to orientation in Axes.grouped_bar() triggers a misleading “ambiguous truth value” error from _api.check_in_list. The function should instead raise a clear ValueError stating th...
Open Graph Description: Bug summary Passing a non-string value (like a NumPy array) to orientation in Axes.grouped_bar() triggers a misleading “ambiguous truth value” error from _api.check_in_list. The function should ins...
X Description: Bug summary Passing a non-string value (like a NumPy array) to orientation in Axes.grouped_bar() triggers a misleading “ambiguous truth value” error from _api.check_in_list. The function should ins...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/30706
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: Axes.grouped_bar() with non-string orientation (e.g., NumPy array) raises ambiguous truth-value error instead of clean ValueError","articleBody":"### Bug summary\n\nPassing a non-string value (like a NumPy array) to orientation in Axes.grouped_bar() triggers a misleading “ambiguous truth value” error from _api.check_in_list. The function should instead raise a clear ValueError stating the value is not valid.\n\n\n### Code for reproduction\n\n```Python\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfig, ax = plt.subplots()\nax.grouped_bar([[1, 2, 3], [3, 2, 1]], orientation=np.array([1, 2, 3]))\n```\n\n### Actual outcome\n\nAxes.grouped_bar() fails inside _api.check_in_list() because NumPy arrays cannot be evaluated in a boolean context.\nInstead of raising a clear validation error, it produces a misleading NumPy truth-value error, causing the test to fail.\n\n\u003cimg width=\"1600\" height=\"1172\" alt=\"Image\" src=\"https://github.com/user-attachments/assets/0a24242f-ae50-45ba-a15e-af44768f4333\" /\u003e\n\n### Expected outcome\n\nAfter applying the fix, invalid inputs (including NumPy arrays) are handled gracefully, and all tests expecting a clear ValueError now pass.\n\n\n\n### Additional information\n\n\n1. Conditions under which this bug happens:\n\nThe bug occurs whenever the orientation parameter passed to Axes.grouped_bar() is not a string, such as:\norientation = np.array([1, 2, 3]) # NumPy array \norientation = 1 # integer \norientation = None # NoneType\n\nThese types trigger an internal truth-value evaluation inside _api.check_in_list, which expects a scalar comparable value (like a string).\n\n2. Edge cases affected:\n\nNumPy arrays (np.array([...]))\nNon-string types (integers, floats, None, lists, etc.)\nAny custom object that doesn’t implement __eq__ safely with strings\n\n3. Behavior in earlier versions:\n\nThis issue has likely existed since the introduction of Axes.grouped_bar() (Matplotlib 3.11, provisional API).\nOther similar Matplotlib APIs (e.g., barh, stem, etc.) already validate string-type enums, so they are not affected.\n\n4. Root cause (why this happens):\n\nThe function directly calls:\n\n_api.check_in_list([\"vertical\", \"horizontal\"], orientation=orientation)\n\nWhen orientation is a NumPy array, the expression val not in values inside check_in_list() performs elementwise comparison, returning an array of booleans.\n\nPython then attempts to interpret that array as a single truth value, which triggers NumPy’s error:\nValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().\n\n5. Proposed fix (confirmed working):\n\nAdd an early type guard before calling _api.check_in_list():\n\nif not isinstance(orientation, str):\n raise ValueError(f\"{orientation!r} is not a valid value for orientation\")\n_api.check_in_list([\"vertical\", \"horizontal\"], orientation=orientation)\n\n-- This ensures non-string inputs are rejected immediately and consistently,\n-- prevents the ambiguous truth-value error, and\n-- aligns grouped_bar() with Matplotlib’s standard API validation behavior.\n\n### Operating system\n\nMacOS\n\n### Matplotlib Version\n\n3.11.0.dev1446+g319295e28.d20251030\n\n### Matplotlib Backend\n\nmacosx\n\n### Python version\n\nPython 3.13.7\n\n### Jupyter version\n\n_No response_\n\n### Installation\n\npip","author":{"url":"https://github.com/ilakkmanoharan","@type":"Person","name":"ilakkmanoharan"},"datePublished":"2025-10-30T20:25:35.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/30706/matplotlib/issues/30706"}
| 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:79a8c22a-2ad1-efa9-59f9-7af3111f7173 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A70A:292960:4D4FED:69951D:6969DE1B |
| html-safe-nonce | 9b51a257a38b8900139ccc2316729b0d5565d909eb565b90abd7291f498f0ef4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzBBOjI5Mjk2MDo0RDRGRUQ6Njk5NTFEOjY5NjlERTFCIiwidmlzaXRvcl9pZCI6IjYxNzk4OTUxMTk2MDgxNDMzODciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 061ea3307c0e344a1c8bc2d3bf1912f2ac828d5af084b3e863da1dfc2f449e7d |
| hovercard-subject-tag | issue:3572386499 |
| 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/30706/issue_layout |
| twitter:image | https://opengraph.githubassets.com/44e11f00907848d57ecee7895ca5d3f57c1192b55be175d7c5b5593c81cc1f24/matplotlib/matplotlib/issues/30706 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/44e11f00907848d57ecee7895ca5d3f57c1192b55be175d7c5b5593c81cc1f24/matplotlib/matplotlib/issues/30706 |
| og:image:alt | Bug summary Passing a non-string value (like a NumPy array) to orientation in Axes.grouped_bar() triggers a misleading “ambiguous truth value” error from _api.check_in_list. The function should ins... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ilakkmanoharan |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7b32f1c7c4549428ee399213e8345494fc55b5637195d3fc5f493657579235e8 |
| 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 | bdde15ad1b403e23b08bbd89b53fbe6bdf688cad |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width