Raise ValueError for non-string orientation in Axes.grouped_bar (#30706) #30707
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR summary
This PR fixes issue #30706.
Why this change is necessary
Passing a non-string value (e.g.,
np.array,int,None) to theorientationparameter of
Axes.grouped_bar()previously caused a misleading NumPy error:ValueError: The truth value of an array with more than one element is ambiguous
This occurred because
_api.check_in_list()attempted a membership check on anon-scalar object, leading to an elementwise boolean comparison.
What problem it solves
This patch ensures that invalid (non-string)
orientationvalues raise a clean,user-facing
ValueErrorinstead of an ambiguous NumPy truth-value error.This behavior now matches other Matplotlib functions (e.g.,
barh,stem)that first validate enum arguments before list membership checks.
Implementation details
Added an early type guard in
Axes.grouped_bar()before calling_api.check_in_list():Tests
Added a new test test_grouped_bar_orientation_invalid() to
lib/matplotlib/tests/test_axes.py verifying the following cases:
"invalid"
1
None
np.array([1, 2, 3])
Each now raises:
ValueError: '' is not a valid value for orientation
Result
Clean ValueError instead of ambiguous truth-value error
Consistent behavior with other Matplotlib APIs
All tests pass locally
PR checklist
closes #30706
new and changed code is tested
[N/A] Plotting related features are demonstrated in an example
[N/A] New Features and API Changes are noted with a directive and release note
[N/A] Documentation complies with general and docstring guidelines