I was wondering if it is possible to change the transparency of a text in Matplotlib. set_alpha
does not function and in the documentation I couldn't find anything relevant. Are there may be any workarounds?
I want to connect it to a pick_event.
EDIT:
I was actually trying to change the transparency of a legend-text. Although I tried to solve the issue with set_alpha
, I have overseen that I was trying to modify the transparency of a list and hence I couldn't succeed. To sum up, as can be seen from answers, the transparency can be modified with set_alpha
Matplotlib allows you to regulate the transparency of a graph plot using the alpha attribute. By default, alpha=1. If you would like to form the graph plot more transparent, then you'll make alpha but 1, such as 0.5 or 0.25.
Matplotlib allows you to adjust the transparency of a graph plot using the alpha attribute. If you want to make the graph plot more transparent, then you can make alpha less than 1, such as 0.5 or 0.25. If you want to make the graph plot less transparent, then you can make alpha greater than 1.
You can set alpha when using annotate
to add the text to your figure.
Before:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.annotate("TESTING", xy=(.5, .5), xytext=(.5, .5))
plt.show()
After:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
text = ax.annotate("TESTING", xy=(.5, .5), xytext=(.5, .5))
text.set_alpha(.4)
plt.show()
If you want to set alpha on the legend text, you should have said so:
ax.plot([1,2,3], [4,5,6], label='Null')
leg = ax.legend()
# print dir(leg) # inspection
for _txt in leg.texts:
_txt.set_alpha(0.3)
Side note: Because I can never remember where exactly to find things in the mpl docs, I inspected the legend object. Attribute texts
sounded useful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With