Skip to content

Setting alpha on legend handle changes patch color #11702

Closed

Description

@bpteague

Bug report

Bug summary

I occasionally want the alpha value of the patch in a histogram's legend to be different than the alpha value used to plot the legend. However, when I call set_alpha on a legend handle, the color of the patch in the legend changes.

Code for reproduction

import matplotlib.pyplot as plt

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)

Actual outcome

image

Work-around

I can get the result I desire if I call set_edgecolor() and set_facecolor():

plt.hist([1, 2, 3], alpha = 0.25, label = 'data', color = 'red')
legend = plt.legend()
for lh in legend.legendHandles:
    lh.set_facecolor(lh.get_facecolor())
    lh.set_edgecolor(lh.get_edgecolor())
    lh.set_alpha(1.0)

Expected outcome

image

Matplotlib version

  • Matplotlib v2.2.2
  • Installed from Anaconda (default channel)
  • Backend: module://ipykernel.pylab.backend_inline
  • Jupyter version: 5.5.0

Activity

  1. bpteague commented on Jul 19, 2018

    @bpteague
    Author

    Seems related to the following lines in matplotlib.patches.set_alpha:

            self._set_facecolor(self._original_facecolor)
            self._set_edgecolor(self._original_edgecolor)

    because both self._original_facecolor and self._original_edgecolor are set to None.

  2. jklymak commented on Jul 19, 2018

    @jklymak
    Member

    Huh, I thought we squashed that bug. Anyway, the fix is to have the _original_face/edgecolors set in set_color few lines after. Want to make a PR?

  3. bpteague commented on Jul 19, 2018

    @bpteague
    Author

    It looks like set_color() calls set_edgecolor() and set_facecolor(), which already update _original_edgecolor and _original_facecolor respectively.

    Perhaps set_color() isn't getting called as the legend is being set up? I tried digging through the legend code, but I don't understand matplotlib's internals terribly well and got rather lost.

  4. jklymak commented on Jul 19, 2018

    @jklymak
    Member

    Ooops. Yes, you are right. Chasing through legend isn't pretty...

  5. jklymak commented on Jul 19, 2018

    @jklymak
    Member

    See #11703 - looks like it fixes the problem to me... Thanks for the great report.

  6. bpteague commented on Jul 19, 2018

    @bpteague
    Author

    Brilliant. #11703 fixes this for me too. Thanks for digging into this for me (-:

  7. jklymak commented on Jul 19, 2018

    @jklymak
    Member

    Ha we only get to close it when the PR gets merged 😉

  8. bpteague commented on Jul 19, 2018

    @bpteague
    Author

    I just got so excited! An hour from report to fix is pretty remarkable. Thanks again.

  9. jklymak commented on Jul 19, 2018

    @jklymak
    Member

    Well, I was annoyed, because I fixed that bug 6 months ago (for markers instead of bars)...

  10. added this to the v3.0 milestone on Jul 27, 2018
  11. boyob commented on Apr 3, 2019

    @boyob

    1
    How to change the color of font in legend?

  12. timhoffm commented on Apr 3, 2019

    @timhoffm
    Member

    @boyob This is the bug tracker. For help, please use gitter, the mailinglist or stackoverflow. https://matplotlib.org/#need-help

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions