Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 08f53e5

Browse filesBrowse files
committed
Improve error message for shape mismatches in bar function
1 parent f39bf4d commit 08f53e5
Copy full SHA for 08f53e5

File tree

Expand file treeCollapse file tree

2 files changed

+31
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+31
-4
lines changed

‎lib/matplotlib/axes/_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+21-4Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9635,3 +9635,13 @@ def test_axes_set_position_external_bbox_unchanged(fig_test, fig_ref):
96359635
ax_test.set_position([0.25, 0.25, 0.5, 0.5])
96369636
assert (bbox.x0, bbox.y0, bbox.width, bbox.height) == (0.0, 0.0, 1.0, 1.0)
96379637
ax_ref = fig_ref.add_axes([0.25, 0.25, 0.5, 0.5])
9638+
9639+
9640+
def test_bar_shape_mismatch():
9641+
x = ["foo", "bar"]
9642+
height = [1, 2, 3]
9643+
error_message = (
9644+
r"Mismatch is between 'x' with shape \(2,\) and 'height' with shape \(3,\)"
9645+
)
9646+
with pytest.raises(ValueError, match=error_message):
9647+
plt.bar(x, height)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.