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

Code before:
    pc = mcollections.PatchCollection(
        [
            mpatches.Polygon([[0, 0], [100, 100], [200, 0]]),
            mpatches.Polygon([[400, 0], [500, 100], [600, 0]]),
        ],
        label="patch collection", facecolor='blue', edgecolor='black'
    )
    ax.add_collection(pc, autolim=True)
    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 colors
    assert len(leg.legend_handles) == 1
    legend_patch = leg.legend_handles[0]
    assert_allclose(legend_patch.get_facecolor(),
                    pc.get_facecolor()[0], rtol=1e-5)
    assert_allclose(legend_patch.get_edgecolor(),
                    pc.get_edgecolor()[0], rtol=1e-5)


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

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

    leg = ax.legend()

    # Verify that visual properties are preserved
    legend_patch = leg.legend_handles[0]
    assert_allclose(legend_patch.get_facecolor(),
                    pc.get_facecolor()[0], rtol=1e-5)
    assert_allclose(legend_patch.get_edgecolor(),
                    pc.get_edgecolor()[0], rtol=1e-5)
    assert legend_patch.get_linewidth() == pc.get_linewidths()[0]
    assert legend_patch.get_linestyle() == pc.get_linestyles()[0]


Review comment (suggestion):
You can combine `test_patchcollection_legend:` and `test_patchcollection_legend_properties():` b/c text is just another property

Code after:

    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