Repository: matplotlib/matplotlib
File: lib/matplotlib/tests/test_legend.py (Python)

Code before:
    # Test that PatchCollection labels show up in legend (issue #23998)
    fig, ax = plt.subplots()

    # Create some patches
    p1 = mpatches.Polygon([[0, 0], [100, 100], [200, 0]])
    p2 = mpatches.Polygon([[400, 0], [500, 100], [600, 0]])

    # Create a PatchCollection with a label
    pc = mcollections.PatchCollection([p1, p2], label="patch collection",
                                      facecolor='blue', edgecolor='black')
    ax.add_collection(pc, autolim=True)
    ax.autoscale_view()

    # Create the legend
    leg = ax.legend()

    # Check that the legend contains our label
    assert len(leg.get_texts()) == 1
    assert leg.get_texts()[0].get_text() == "patch collection"

    # Check that there's a legend handle
    handles = leg.legend_handles
    assert len(handles) == 1
    assert isinstance(handles[0], mpatches.Rectangle)

    plt.close(fig)


def test_patchcollection_legend_properties():
    # Test that PatchCollection legend preserves visual properties
    fig, ax = plt.subplots()

    # Create patches with specific properties
    p1 = mpatches.Circle((0, 0), 1)
    p2 = mpatches.Circle((2, 0), 1)

    # Create a PatchCollection with specific visual properties
    pc = mcollections.PatchCollection(
        [p1, p2],
        label="styled patches",
        facecolor='red',
        edgecolor='blue',
        linewidths=3,
        linestyle='--'
    )
    ax.add_collection(pc)
    ax.autoscale_view()

    # Create the legend
    leg = ax.legend()
    legend_patch = leg.legend_handles[0]

Review comment (suggestion):
```suggestion
```
Unneeded. Tests are autotmatically cleaned up.

Code after:
    # Test that PatchCollection labels show up in legend and preserve visual
    # properties (issue #23998)
    fig, ax = plt.subplots()

    pc = mcollections.PatchCollection(
        [mpatches.Circle((0, 0), 1), mpatches.Circle((2, 0), 1)],
        label="patch collection",
        facecolor='red',
        edgecolor='blue',
        linewidths=3,
        linestyle='--',
    )
    ax.add_collection(pc)
    ax.autoscale_view()

    leg = ax.legend()

    # Check that the legend contains our label
    assert len(leg.get_texts()) == 1
    assert leg.get_texts()[0].get_text() == "patch collection"

    # Check that the legend handle exists and has correct visual properties
    assert len(leg.legend_handles) == 1
    legend_patch = leg.legend_handles[0]
    assert mpl.colors.same_color(legend_patch.get_facecolor(),
                                  pc.get_facecolor()[0])
    assert mpl.colors.same_color(legend_patch.get_edgecolor(),
                                  pc.get_edgecolor()[0])
    assert legend_patch.get_linewidth() == pc.get_linewidths()[0]
    assert legend_patch.get_linestyle() == pc.get_linestyles()[0]


def test_patchcollection_legend_empty():
    # Test that empty PatchCollection doesn't crash
    fig, ax = plt.subplots()

    # Create an empty PatchCollection
    pc = mcollections.PatchCollection([], label="empty collection")
    ax.add_collection(pc)

    # This should not crash
    leg = ax.legend()

    # Check that the label still appears
    assert len(leg.get_texts()) == 1
    assert leg.get_texts()[0].get_text() == "empty collection"

    # The legend handle should exist
    assert len(leg.legend_handles) == 1