Skip to content

Commit 92b643f

Browse files
authored
Merge pull request #24431 from timhoffm/eventplot-alpha
FIX: Support passing one alpha per event sequence to eventplot()
2 parents 7cd9c1b + 81b38da commit 92b643f

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

lib/matplotlib/axes/_axes.py

+22-7
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
@@ -1273,8 +1280,8 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
12731280
12741281
Notes
12751282
-----
1276-
For *linelengths*, *linewidths*, *colors*, and *linestyles*, if only
1277-
a single value is given, that value is applied to all lines. If an
1283+
For *linelengths*, *linewidths*, *colors*, *alpha* and *linestyles*, if
1284+
only a single value is given, that value is applied to all lines. If an
12781285
array-like is given, it must have the same length as *positions*, and
12791286
each value will be applied to the corresponding row of the array.
12801287
@@ -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/pyplot.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2528,12 +2528,12 @@ def errorbar(
25282528
@_copy_docstring_and_deprecators(Axes.eventplot)
25292529
def eventplot(
25302530
positions, orientation='horizontal', lineoffsets=1,
2531-
linelengths=1, linewidths=None, colors=None,
2531+
linelengths=1, linewidths=None, colors=None, alpha=None,
25322532
linestyles='solid', *, data=None, **kwargs):
25332533
return gca().eventplot(
25342534
positions, orientation=orientation, lineoffsets=lineoffsets,
25352535
linelengths=linelengths, linewidths=linewidths, colors=colors,
2536-
linestyles=linestyles,
2536+
alpha=alpha, linestyles=linestyles,
25372537
**({"data": data} if data is not None else {}), **kwargs)
25382538

25392539

lib/matplotlib/tests/test_axes.py

+20
Original file line numberDiff line numberDiff line change
@@ -4562,6 +4562,26 @@ def test_eventplot_colors(colors):
45624562
assert_allclose(coll.get_color(), color)
45634563

45644564

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

0 commit comments

Comments
 (0)