Skip to content

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

Closed

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

Activity

  1. jklymak commented on Dec 15, 2021

    @jklymak
    Member

    The test in #11703 is still in the code base and passing, so I'm not sure how this could still be a bug. Can you reproduce w/o pandas?

    def test_alpha_handles():
        x, n, hh = plt.hist([1, 2, 3], alpha=0.25, label='data', color='red')
        legend = plt.legend()
        for lh in legend.legendHandles:
            lh.set_alpha(1.0)
        assert lh.get_facecolor()[:-1] == hh[1].get_facecolor()[:-1]
        assert lh.get_edgecolor()[:-1] == hh[1].get_edgecolor()[:-1]
    

    Is it something subtle about the difference between setting the handles and the patches? It should be the same.

  2. wavexx commented on Dec 15, 2021

    @wavexx
    ContributorAuthor
  3. jklymak commented on Dec 15, 2021

    @jklymak
    Member

    This seems like someone who is familiar with what pandas does w their legend needs to help sort this out. I would suggest opening with them if there is no native matplotlib repo?

  4. tacaswell commented on Dec 15, 2021

    @tacaswell
    Member

    I do not expect them to be doing anything fancy, I think we just need to look at what the class of Artist is being used in the plot and what the type of the Artist in the handler is.

    I suspect we are just going to discover that there is another class that needs update_from updated.

  5. StefRe commented on Dec 19, 2021

    @StefRe
    Contributor

    Has nothing to do with pandas, can reproduce it with mpl without pandas. I found the culprit. Will make a PR after checking if this issues shows some else too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions