0

I would like to change the alpha of the legend's labels on Matplotlib.

I tried to get the legend's label's artist and set the alpha.

lh = ax.get_legend_handles_labels()

for label in lh[1]:    
    label.set_alpha(0.6)

I expect the label's alpha to change but it doesn't work. It is not getting the label artist, just the strings: AttributeError: 'str' object has no attribute 'set_alpha'

Dimitry Ernot
  • 6,256
  • 2
  • 25
  • 37
Marcelo P
  • 3
  • 1
  • You want to change the alpha for the label or the handle? Label is the text typically and handle is the line and/or the marker. Which one are you referring to? – Sheldore May 02 '19 at 15:00
  • Possible duplicate of [Set legend symbol opacity with matplotlib?](https://stackoverflow.com/questions/12848808/set-legend-symbol-opacity-with-matplotlib) – Sheldore May 02 '19 at 15:02
  • [This](https://stackoverflow.com/a/24065905/4932316) answer explains it how to do it for the legend strings – Sheldore May 02 '19 at 15:15

1 Answers1

0

Loop through the legend texts and set alpha for each.

import matplotlib.pyplot as plt

for i in range(4):
    plt.plot([0,1],[1,i], label=f"Label {i}")

leg = plt.legend()

for t in leg.texts:
    t.set_alpha(0.3)

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712