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

MNT: Make bar() error handling independent of numpy error message · matplotlib/matplotlib@e2d2eba · GitHub

Repository navigation

Commit e2d2eba

Browse files
committed
MNT: Make bar() error handling independent of numpy error message
as proposed in #32431 (comment)
1 parent 0aeef9d commit e2d2eba

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
@@ -2520,27 +2520,23 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
25202520
height = self._convert_dx(height, y0, y, self.convert_yunits)
25212521
if yerr is not None:
25222522
yerr = self._convert_dx(yerr, y0, y, self.convert_yunits)
2523+
2524+
# broadcast all args to make them iterable
2525+
x = np.atleast_1d(x)
2526+
args = (x, height, width, y, linewidth, hatch)
2527+
arg_names = ('x', 'height', 'width', 'y', 'linewidth', 'hatch')
25232528
try:
2524-
x, height, width, y, linewidth, hatch = np.broadcast_arrays(
2525-
# Make args iterable too.
2526-
np.atleast_1d(x), height, width, y, linewidth, hatch
2527-
)
2529+
x, height, width, y, linewidth, hatch = np.broadcast_arrays(*args)
25282530
except ValueError as e:
2529-
arg_map = {
2530-
"arg 0": "'x'",
2531-
"arg 1": "'height'",
2532-
"arg 2": "'width'",
2533-
"arg 3": "'y'",
2534-
"arg 4": "'linewidth'",
2535-
"arg 5": "'hatch'"
2536-
}
2537-
error_message = str(e)
2538-
for arg, name in arg_map.items():
2539-
error_message = error_message.replace(arg, name)
2540-
if error_message != str(e):
2541-
raise ValueError(error_message) from e
2542-
else:
2543-
raise
2531+
non_trivial_inputs = ", ".join(
2532+
f"{name} {np.shape(arg)}" for arg, name in zip(args, arg_names)
2533+
# only collect args that could contribute to a broadcast error
2534+
if any(dim != 1 for dim in np.shape(arg))
2535+
)
2536+
raise ValueError(
2537+
f"bar() failed on broadcasting inputs with the following shapes: "
2538+
f"{non_trivial_inputs}"
2539+
) from e
25442540

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

‎lib/matplotlib/tests/test_axes.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10411,7 +10411,8 @@ def test_bar_shape_mismatch():
1041110411
x = ["foo", "bar"]
1041210412
height = [1, 2, 3]
1041310413
error_message = (
10414-
r"Mismatch is between 'x' with shape \(2,\) and 'height' with shape \(3,\)"
10414+
r"bar\(\) failed on broadcasting inputs with the following shapes: "
10415+
r"x \(2,\), height \(3,\)"
1041510416
)
1041610417
with pytest.raises(ValueError, match=error_message):
1041710418
plt.bar(x, height)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL