Legend with transparent frame and non-transparent edge

Viewed 16658

When setting the transparency in a legend:

plt.legend(framealpha=0.5)

it affects both the background and the edge. How can I apply transparency only to the background and not to the edge?

I have tried:

plt.legend(edgecolor=(0, 0, 0, 1.), facecolor=(1, 1, 1, 0.1))

with which I can change the color but not the transparency, i.e., using facecolor=(1, 0, 0, 0.1) does change the background color to red but using facecolor=(1, 0, 0, 0.5) doesn't make any difference in transparency.

I'm using matplotlib v3.1.1.

1 Answers

Unfortunately, the framealpha overrides the alpha value of the actual edge- or facecolors. Hence one would need to set the color, including alpha, manually on the legend patch.

legend = plt.legend(loc="upper right", edgecolor="black")
legend.get_frame().set_alpha(None)
legend.get_frame().set_facecolor((0, 0, 1, 0.1))
Related