Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fixed eventplot issues #22286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,9 +1313,6 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
else:
positions = [np.asanyarray(positions)]

if len(positions) == 0:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the code above, this cannot happen.

return []

poss = []
for position in positions:
poss += self._process_unit_info([("x", position)], kwargs)
Expand Down Expand Up @@ -1345,13 +1342,15 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
linewidths = np.asarray(linewidths)

if len(lineoffsets) == 0:
lineoffsets = [None]
raise ValueError('lineoffsets cannot be empty')
if len(linelengths) == 0:
linelengths = [None]
if len(linewidths) == 0:
lineoffsets = [None]
raise ValueError('linelengths cannot be empty')
if len(linestyles) == 0:
raise ValueError('linestyles cannot be empty')
if len(linewidths) == 0:
lineoffsets = [None]
raise ValueError('linewidths cannot be empty')
if len(alpha) == 0:
raise ValueError('alpha cannot be empty')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is added compared to the discussion we had. For similar behavior and it will still break with unequal sizes below.

An option is to skip all these len() == 0 checks, except for colors, and rely on the unequal size below. Fewer checks, but slightly worse error message.

if len(colors) == 0:
colors = [None]
try:
Expand Down
25 changes: 25 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6906,6 +6906,31 @@ def test_eventplot_legend():
plt.legend()


@pytest.mark.parametrize('err, args, kwargs, match', (
(ValueError, [[1]], {'lineoffsets': []}, 'lineoffsets cannot be empty'),
(ValueError, [[1]], {'linelengths': []}, 'linelengths cannot be empty'),
(ValueError, [[1]], {'linewidths': []}, 'linewidths cannot be empty'),
(ValueError, [[1]], {'linestyles': []}, 'linestyles cannot be empty'),
(ValueError, [[1]], {'alpha': []}, 'alpha cannot be empty'),
(ValueError, [1], {}, 'positions must be one-dimensional'),
(ValueError, [[1]], {'lineoffsets': [1, 2]},
'lineoffsets and positions are unequal sized sequences'),
(ValueError, [[1]], {'linelengths': [1, 2]},
'linelengths and positions are unequal sized sequences'),
(ValueError, [[1]], {'linewidths': [1, 2]},
'linewidths and positions are unequal sized sequences'),
(ValueError, [[1]], {'linestyles': [1, 2]},
'linestyles and positions are unequal sized sequences'),
(ValueError, [[1]], {'alpha': [1, 2]},
'alpha and positions are unequal sized sequences'),
(ValueError, [[1]], {'colors': [1, 2]},
'colors and positions are unequal sized sequences'),
))
def test_eventplot_errors(err, args, kwargs, match):
with pytest.raises(err, match=match):
plt.eventplot(*args, **kwargs)


def test_bar_broadcast_args():
fig, ax = plt.subplots()
# Check that a bar chart with a single height for all bars works.
Expand Down