FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #32441 from meeseeksmachine/auto-backport-of-pr-32… · matplotlib/matplotlib@ea33f20 · GitHub

Repository navigation

Commit ea33f20

Browse files
authored
Merge pull request #32441 from meeseeksmachine/auto-backport-of-pr-32440-on-v3.11.x
Backport PR #32440 on branch v3.11.x (MNT: Make bar() error handling independent of numpy error message)
2 parents 717c2bc + 733e7ae commit ea33f20

2 files changed

Lines changed: 17 additions & 20 deletions

File tree

‎lib/matplotlib/axes/_axes.py‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,27 +2519,23 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
25192519
height = self._convert_dx(height, y0, y, self.convert_yunits)
25202520
if yerr is not None:
25212521
yerr = self._convert_dx(yerr, y0, y, self.convert_yunits)
2522+
2523+
# broadcast all args to make them iterable
2524+
x = np.atleast_1d(x)
2525+
args = (x, height, width, y, linewidth, hatch)
2526+
arg_names = ('x', 'height', 'width', 'y', 'linewidth', 'hatch')
25222527
try:
2523-
x, height, width, y, linewidth, hatch = np.broadcast_arrays(
2524-
# Make args iterable too.
2525-
np.atleast_1d(x), height, width, y, linewidth, hatch
2526-
)
2528+
x, height, width, y, linewidth, hatch = np.broadcast_arrays(*args)
25272529
except ValueError as e:
2528-
arg_map = {
2529-
"arg 0": "'x'",
2530-
"arg 1": "'height'",
2531-
"arg 2": "'width'",
2532-
"arg 3": "'y'",
2533-
"arg 4": "'linewidth'",
2534-
"arg 5": "'hatch'"
2535-
}
2536-
error_message = str(e)
2537-
for arg, name in arg_map.items():
2538-
error_message = error_message.replace(arg, name)
2539-
if error_message != str(e):
2540-
raise ValueError(error_message) from e
2541-
else:
2542-
raise
2530+
non_trivial_inputs = ", ".join(
2531+
f"{name} {np.shape(arg)}" for arg, name in zip(args, arg_names)
2532+
# only collect args that could contribute to a broadcast error
2533+
if any(dim != 1 for dim in np.shape(arg))
2534+
)
2535+
raise ValueError(
2536+
f"bar() failed on broadcasting inputs with the following shapes: "
2537+
f"{non_trivial_inputs}"
2538+
) from e
25432539

25442540
# Now that units have been converted, set the tick locations.
25452541
if orientation == 'vertical':

‎lib/matplotlib/tests/test_axes.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10365,7 +10365,8 @@ def test_bar_shape_mismatch():
1036510365
x = ["foo", "bar"]
1036610366
height = [1, 2, 3]
1036710367
error_message = (
10368-
r"Mismatch is between 'x' with shape \(2,\) and 'height' with shape \(3,\)"
10368+
r"bar\(\) failed on broadcasting inputs with the following shapes: "
10369+
r"x \(2,\), height \(3,\)"
1036910370
)
1037010371
with pytest.raises(ValueError, match=error_message):
1037110372
plt.bar(x, height)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL