8000 Merge pull request #29503 from Kaustbh/issue4 · matplotlib/matplotlib@d488794 · GitHub
[go: up one dir, main page]

Skip to content

Commit d488794

Browse files
authored
Merge pull request #29503 from Kaustbh/issue4
Improve error message for shape mismatches in barh function
2 parents 8c3f4ec + 08f53e5 commit d488794

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,10 +2574,27 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
25742574
height = self._convert_dx(height, y0, y, self.convert_yunits)
25752575
if yerr is not None:
25762576
yerr = self._convert_dx(yerr, y0, y, self.convert_yunits)
2577-
2578-
x, height, width, y, linewidth, hatch = np.broadcast_arrays(
2579-
# Make args iterable too.
2580-
np.atleast_1d(x), height, width, y, linewidth, hatch)
2577+
try:
2578+
x, height, width, y, linewidth, hatch = np.broadcast_arrays(
2579+
# Make args iterable too.
2580+
np.atleast_1d(x), height, width, y, linewidth, hatch
2581+
)
2582+
except ValueError as e:
2583+
arg_map = {
2584+
"arg 0": "'x'",
2585+
"arg 1": "'height'",
2586+
"arg 2": "'width'",
2587+
"arg 3": "'y'",
2588+
"arg 4": "'linewidth'",
2589+
"arg 5": "'hatch'"
2590+
}
2591+
error_message = str(e)
2592+
for arg, name in arg_map.items():
2593+
error_message = error_message.replace(arg, name)
2594+
if error_message != str(e):
2595+
raise ValueError(error_message) from e
2596+
else:
2597+
raise
25812598

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

lib/matplotlib/tests/test_axes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9705,3 +9705,13 @@ def test_axes_set_position_external_bbox_unchanged(fig_test, fig_ref):
97059705
ax_test.set_position([0.25, 0.25, 0.5, 0.5])
97069706
assert (bbox.x0, bbox.y0, bbox.width, bbox.height) == (0.0, 0.0, 1.0, 1.0)
97079707
ax_ref = fig_ref.add_axes([0.25, 0.25, 0.5, 0.5])
9708+
9709+
9710+
def test_bar_shape_mismatch():
9711+
x = ["foo", "bar"]
9712+
height = [1, 2, 3]
9713+
error_message = (
9714+
r"Mismatch is between 'x' with shape \(2,\) and 'height' with shape \(3,\)"
9715+
)
9716+
with pytest.raises(ValueError, match=error_message):
9717+
plt.bar(x, height)

0 commit comments

Comments
 (0)
0