Skip to content

Transparency not respected in legendPatch #3766

Closed

Description

@mrclary

It seems after Matplotlib 1.3.1 (1.4.1, 1.4.2, not sure about 1.4.0) that setting the transparency of the facecolor or edgecolor properties of a legendPatch is not respected. Both properties can be changed together using legendPatch.set_alpha(...), but they cannot be set independently. Following is a MWE. Here, the legend's facecolor is changed, but the alpha specification (0.2) is ignored. A call to legendPatch.get_facecolor() confirms this.

import matplotlib.pyplot as plt

plt.plot(range(10))
leg = plt.legend(['Line'])
leg.legendPatch.set_facecolor([0.5,1,0.5,0.2])

Activity

  1. added this to the v1.4.3 milestone on Nov 9, 2014
  2. tacaswell commented on Nov 9, 2014

    @tacaswell
    Member

    I am a bit preplexed by this. It looks like the patch attached to the legend is a FancyBboxPatch which uses the set_facecolor method from Patch. The color is rgba-ified using colors.colorConverter.to_rgba(arg, self._alpha) which will ignore the alpha value passed in (the observed issue). However, looking at the blame for both of those functions, the only changes between 1.3.1 and 1.4 are from #3092 which should not have affected this.

  3. efiring commented on Nov 9, 2014

    @efiring
    Member

    853c26a
    calls set_alpha() on the patch; once that is set, set_facecolor uses it to override the "a" in "rgba".

    In other words, adding an rcParam changed _alpha of None to a default value; and once that is done, you are stuck with it until you explicitly change it! The painful saga of alpha-handling continues...

    As a matter of style, the relevant bit of code in legend:

            if framealpha is None:
                self.get_frame().set_alpha(rcParams["legend.framealpha"])
            else:
                self.get_frame().set_alpha(framealpha)

    is obfuscated by the use of the get_frame() method; what it gets is the legendPatch that was created in the same method a few lines above.

  4. mrclary commented on Nov 9, 2014

    @mrclary
    Author

    I've confirmed that the behavior exhibits after version 1.4.0 (1.3.1 and 1.4.0 are okay, >=1.4.1 not okay)

  5. tacaswell commented on Nov 9, 2014

    @tacaswell
    Member

    I have a fix for this, just need to add some tests.

  6. added a commit that references this issue on Nov 9, 2014
    09191a4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions