Skip to content

[Bug]: Regression: Setting alpha on legend handle changes patch color #21966

@wavexx

Description

@wavexx

Bug summary

Bug #11702 seems to have reappeared: calling set_alpha on a legend handle causes the color of the patch in the legend to change in a filled area plot.

Code for reproduction

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({
    'sales': [3, 2, 3],
    'visits': [20, 42, 28],
    'day': [1, 2, 3],
})

df.plot.area(x='day')
legend = plt.legend()
for p in legend.get_patches():
    p.set_alpha(1)

Actual outcome

2021-12-15T173908

Expected outcome

The patch for 'visits' should be orange.

Additional information

This was working fine at least until mpl 3.3.4.

As for the previous workaround, calling set_facecolor(get_facecolor()) before calling set_alpha() works. I'm using legend.get_patches() here instead of iterating through the handles directly, but there's no difference.

Operating system

Linux (debian unstable)

Matplotlib Version

3.5.0

Matplotlib Backend

Qt5Agg or agg

Python version

3.9.9

Jupyter version

n/a

Installation

Linux package manager

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions