Skip to content

Commit 5b72946

Browse files
committed
FIX: Support passing one alpha per event sequence to eventplot()
1 parent cd185ab commit 5b72946

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/matplotlib/axes/_axes.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',
11841184
"colors", "linestyles"])
11851185
@_docstring.dedent_interpd
11861186
def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
1187-
linelengths=1, linewidths=None, colors=None,
1187+
linelengths=1, linewidths=None, colors=None, alpha=None,
11881188
linestyles='solid', **kwargs):
11891189
"""
11901190
Plot identical parallel lines at the given positions.
@@ -1246,6 +1246,12 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
12461246
If *positions* is 2D, this can be a sequence with length matching
12471247
the length of *positions*.
12481248
1249+
alpha : float or array-like, default: 1
1250+
The alpha blending value(s), between 0 (transparent) and 1 (opaque).
1251+
1252+
If *positions* is 2D, this can be a sequence with length matching
1253+
the length of *positions*.
1254+
12491255
linestyles : str or tuple or list of such values, default: 'solid'
12501256
Default is 'solid'. Valid strings are ['solid', 'dashed',
12511257
'dashdot', 'dotted', '-', '--', '-.', ':']. Dash tuples
@@ -1316,6 +1322,8 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
13161322
linewidths = [linewidths]
13171323
if not np.iterable(colors):
13181324
colors = [colors]
1325+
if not np.iterable(alpha):
1326+
alpha = [alpha]
13191327
if hasattr(linestyles, 'lower') or not np.iterable(linestyles):
13201328
linestyles = [linestyles]
13211329

@@ -1352,8 +1360,9 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
13521360
if len(linewidths) == 1:
13531361
linewidths = np.tile(linewidths, len(positions))
13541362
if len(colors) == 1:
1355-
colors = list(colors)
1356-
colors = colors * len(positions)
1363+
colors = list(colors) * len(positions)
1364+
if len(alpha) == 1:
1365+
alpha = list(alpha) * len(positions)
13571366
if len(linestyles) == 1:
13581367
linestyles = [linestyles] * len(positions)
13591368

@@ -1369,20 +1378,25 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
13691378
if len(colors) != len(positions):
13701379
raise ValueError('colors and positions are unequal sized '
13711380
'sequences')
1381+
if len(alpha) != len(positions):
1382+
raise ValueError('alpha and positions are unequal sized '
1383+
'sequences')
13721384
if len(linestyles) != len(positions):
13731385
raise ValueError('linestyles and positions are unequal sized '
13741386
'sequences')
13751387

13761388
colls = []
1377-
for position, lineoffset, linelength, linewidth, color, linestyle in \
1389+
for position, lineoffset, linelength, linewidth, color, alpha_, \
1390+
linestyle in \
13781391
zip(positions, lineoffsets, linelengths, linewidths,
1379-
colors, linestyles):
1392+
colors, alpha, linestyles):
13801393
coll = mcoll.EventCollection(position,
13811394
orientation=orientation,
13821395
lineoffset=lineoffset,
13831396
linelength=linelength,
13841397
linewidth=linewidth,
13851398
color=color,
1399+
alpha=alpha_,
13861400
linestyle=linestyle)
13871401
self.add_collection(coll, autolim=False)
13881402
coll._internal_update(kwargs)

lib/matplotlib/tests/test_axes.py

+20
Original file line numberDiff line numberDiff line change
@@ -4559,6 +4559,26 @@ def test_eventplot_colors(colors):
45594559
assert_allclose(coll.get_color(), color)
45604560

45614561

4562+
def test_eventplot_alpha():
4563+
fig, ax = plt.subplots()
4564+
4565+
# one alpha for all
4566+
collections = ax.eventplot([[0, 2, 4], [1, 3, 5, 7]], alpha=0.7)
4567+
assert collections[0].get_alpha() == 0.7
4568+
assert collections[1].get_alpha() == 0.7
4569+
4570+
# one alpha per collection
4571+
collections = ax.eventplot([[0, 2, 4], [1, 3, 5, 7]], alpha=[0.5, 0.7])
4572+
assert collections[0].get_alpha() == 0.5
4573+
assert collections[1].get_alpha() == 0.7
4574+
4575+
with pytest.raises(ValueError, match="alpha and positions are unequal"):
4576+
ax.eventplot([[0, 2, 4], [1, 3, 5, 7]], alpha=[0.5, 0.7, 0.9])
4577+
4578+
with pytest.raises(ValueError, match="alpha and positions are unequal"):
4579+
ax.eventplot([0, 2, 4], alpha=[0.5, 0.7])
4580+
4581+
45624582
@image_comparison(['test_eventplot_problem_kwargs.png'], remove_text=True)
45634583
def test_eventplot_problem_kwargs(recwarn):
45644584
"""

0 commit comments

Comments
 (0)