From 6c3f731d2b99f2993b0888edf195064bf442bd0d Mon Sep 17 00:00:00 2001 From: Eden Rochman Date: Mon, 8 Jun 2026 09:57:28 +0200 Subject: [PATCH] FIX: disable pixel snap in eventplot for large event counts When more than 100 event groups are plotted, the Agg renderer snaps thin lines to the nearest pixel. With many closely spaced rows the rounding can collapse distinct events onto the same pixel, making them invisible. Disable snap automatically above 100 groups unless the caller passes snap explicitly. Closes #20243 --- lib/matplotlib/axes/_axes.py | 2 ++ lib/matplotlib/tests/test_axes.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 6102fd3d3ed3..a2a888f0bee3 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -1517,6 +1517,8 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1, linestyle=linestyle) self.add_collection(coll, autolim=False) coll._internal_update(kwargs) + if 'snap' not in kwargs and len(positions) > 100: + coll.set_snap(False) colls.append(coll) if len(positions) > 0: diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 2e3abec8363e..a4fe5c7e5eda 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8190,6 +8190,27 @@ def test_eventplot_errors(err, args, kwargs, match): plt.eventplot(*args, **kwargs) +def test_eventplot_snap_threshold(): + """Snap is disabled when there are more than 100 event groups.""" + fig, ax = plt.subplots() + positions = [np.random.rand(5) for _ in range(150)] + colls = ax.eventplot(positions) + for coll in colls: + assert coll.get_snap() is False + + fig2, ax2 = plt.subplots() + positions_small = [np.random.rand(5) for _ in range(50)] + colls_small = ax2.eventplot(positions_small) + for coll in colls_small: + assert coll.get_snap() is not False + + fig3, ax3 = plt.subplots() + positions_explicit = [np.random.rand(5) for _ in range(150)] + colls_explicit = ax3.eventplot(positions_explicit, snap=True) + for coll in colls_explicit: + assert coll.get_snap() is True + + def test_bar_broadcast_args(): fig, ax = plt.subplots() # Check that a bar chart with a single height for all bars works.