Skip to content

Commit fdf02fc

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

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

lib/matplotlib/axes/_axes.py

+20-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,13 @@ 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
1251+
(opaque).
1252+
1253+
If *positions* is 2D, this can be a sequence with length matching
1254+
the length of *positions*.
1255+
12491256
linestyles : str or tuple or list of such values, default: 'solid'
12501257
Default is 'solid'. Valid strings are ['solid', 'dashed',
12511258
'dashdot', 'dotted', '-', '--', '-.', ':']. Dash tuples
@@ -1316,6 +1323,8 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
13161323
linewidths = [linewidths]
13171324
if not np.iterable(colors):
13181325
colors = [colors]
1326+
if not np.iterable(alpha):
1327+
alpha = [alpha]
13191328
if hasattr(linestyles, 'lower') or not np.iterable(linestyles):
13201329
linestyles = [linestyles]
13211330

@@ -1352,8 +1361,9 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
13521361
if len(linewidths) == 1:
13531362
linewidths = np.tile(linewidths, len(positions))
13541363
if len(colors) == 1:
1355-
colors = list(colors)
1356-
colors = colors * len(positions)
1364+
colors = list(colors) * len(positions)
1365+
if len(alpha) == 1:
1366+
alpha = list(alpha) * len(positions)
13571367
if len(linestyles) == 1:
13581368
linestyles = [linestyles] * len(positions)
13591369

@@ -1369,20 +1379,25 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
13691379
if len(colors) != len(positions):
13701380
raise ValueError('colors and positions are unequal sized '
13711381
'sequences')
1382+
if len(alpha) != len(positions):
1383+
raise ValueError('alpha and positions are unequal sized '
1384+
'sequences')
13721385
if len(linestyles) != len(positions):
13731386
raise ValueError('linestyles and positions are unequal sized '
13741387
'sequences')
13751388

13761389
colls = []
1377-
for position, lineoffset, linelength, linewidth, color, linestyle in \
1390+
for position, lineoffset, linelength, linewidth, color, alpha_, \
1391+
linestyle in \
13781392
zip(positions, lineoffsets, linelengths, linewidths,
1379-
colors, linestyles):
1393+
colors, alpha, linestyles):
13801394
coll = mcoll.EventCollection(position,
13811395
orientation=orientation,
13821396
lineoffset=lineoffset,
13831397
linelength=linelength,
13841398
linewidth=linewidth,
13851399
color=color,
1400+
alpha=alpha_,
13861401
linestyle=linestyle)
13871402
self.add_collection(coll, autolim=False)
13881403
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)