Skip to content

[Bug]: Inconsistent line anti-aliasing in PolyCollection #30549

@lganic

Description

@lganic

Bug summary

When running a poly collection, depending on the order of points (which shouldn't matter for < 4 points) the line anti-aliasing flips, which creates a different image. (So, creating pytests based on images where the point ordering is non-deterministic doesn't work)

Code for reproduction

See this repo, where I have laid out a testing env that reproduces the effect

https://github.com/lganic/matplotlib-collection-alias-testing.git

However this is the pertinent bit:

@pytest.mark.mpl_image_compare
def test_collection():
    # Define one triangle
    stuff = [[0, 0], [1, 0], [1, 1]]

    random.shuffle(stuff)

    # stuff = [[0, 0], [1, 0], [1, 1]] # Running with this commented out, will usually result in a different image. 

    polys = [np.array(stuff)]
    print(polys)

    fig, ax = plt.subplots()
    ax.set_aspect('equal')

    # Add shaded triangle
    coll = PolyCollection(polys, facecolors="lightblue", edgecolors="k", linewidths=1)
    ax.add_collection(coll)

    # Plot vertices
    pts = np.vstack(polys)  # stack back into N×2 array for scatter
    ax.scatter(pts[:, 0], pts[:, 1], c='red', zorder=5)

    return fig

Actual outcome

Zoomed in diff:

Image

Diff highlighted in red:

Image

Expected outcome

Both produced lines should have matching anti-aliasing.

Additional information

No response

Operating system

Windows 11

Matplotlib Version

3.10.6

Matplotlib Backend

qtagg

Python version

3.13.1

Jupyter version

N/A

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions