Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib text transparency

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

like image 279
T-800 Avatar asked Jun 05 '14 15:06

T-800


People also ask

How do I change the transparency in Matplotlib?

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.

How do you change opacity in Python?

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.


2 Answers

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()

enter image description here

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()

enter image description here

like image 87
The Dude Avatar answered Nov 12 '22 03:11

The Dude


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.

like image 28
nepix32 Avatar answered Nov 12 '22 02:11

nepix32