I can change the opacity of an item in the legend of a scatter plot
import matplotlib.pyplot as plt
# note the alpha value vvvvvvvvvv
plt.scatter(range(10), range(10), alpha=0.15, label='Mount Resegone')
plt.scatter(range(10), [0]*10, alpha=0.15, label='Rimini Beach')
plt.gca().get_legend_handles_labels()[0][0].set_alpha(1.0)
plt.legend()
plt.show()
but doing that I change also the opacity of every dot in the scatter plot.
I guess that there is a single instance of the Patch, correct me if I'm wrong, but my issue is changing the Patch in the legend leaving alone the Patch in the scatter plot.
Addendum
The solution originally suggested (handle._legmarker.set_alpha(1)
) does not work in recent Matplotlib, raising an AttributeError
'PathCollection' object has no attribute '_legmarker'
Fortunately it's even easier to have the expected behaviour
plt.scatter(range(10), range(10), alpha=0.15, label='Mount Resegone')
plt.scatter(range(10), [0]*10, alpha=0.15, label='Rimini Beach')
plt.legend().legendHandles[0].set_alpha(1.0)