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

Backport PR #32440 on branch v3.11.x (MNT: Make bar() error handling independent of numpy error message) by meeseeksmachine · Pull Request #32441 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
34 changes: 15 additions & 19 deletions lib/matplotlib/axes/_axes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2519,27 +2519,23 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
height = self._convert_dx(height, y0, y, self.convert_yunits)
if yerr is not None:
yerr = self._convert_dx(yerr, y0, y, self.convert_yunits)

# broadcast all args to make them iterable
x = np.atleast_1d(x)
args = (x, height, width, y, linewidth, hatch)
arg_names = ('x', 'height', 'width', 'y', 'linewidth', 'hatch')
try:
x, height, width, y, linewidth, hatch = np.broadcast_arrays(
# Make args iterable too.
np.atleast_1d(x), height, width, y, linewidth, hatch
)
x, height, width, y, linewidth, hatch = np.broadcast_arrays(*args)
except ValueError as e:
arg_map = {
"arg 0": "'x'",
"arg 1": "'height'",
"arg 2": "'width'",
"arg 3": "'y'",
"arg 4": "'linewidth'",
"arg 5": "'hatch'"
}
error_message = str(e)
for arg, name in arg_map.items():
error_message = error_message.replace(arg, name)
if error_message != str(e):
raise ValueError(error_message) from e
else:
raise
non_trivial_inputs = ", ".join(
f"{name} {np.shape(arg)}" for arg, name in zip(args, arg_names)
# only collect args that could contribute to a broadcast error
if any(dim != 1 for dim in np.shape(arg))
)
raise ValueError(
f"bar() failed on broadcasting inputs with the following shapes: "
f"{non_trivial_inputs}"
) from e

# Now that units have been converted, set the tick locations.
if orientation == 'vertical':
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/tests/test_axes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -10365,7 +10365,8 @@ def test_bar_shape_mismatch():
x = ["foo", "bar"]
height = [1, 2, 3]
error_message = (
r"Mismatch is between 'x' with shape \(2,\) and 'height' with shape \(3,\)"
r"bar\(\) failed on broadcasting inputs with the following shapes: "
r"x \(2,\), height \(3,\)"
)
with pytest.raises(ValueError, match=error_message):
plt.bar(x, height)
Expand Down
Loading

Back | FazBrowse Home | New Git URL