F. Jehn
F. Jehn

Reputation: 377

Changing alpha value of ticklabels without specifying text

Matplotlib allows changing the alpha value of almost anything, but how does it work for an ticklabel? If I have a text, it is easy:

ax.set_xticklabel(labels, alpha=alpha)

The case is different if I do not have a text as the following throws a TypeError, due to missing labels.

ax.set_xticklabel(alpha=alpha)

Therefore, my next idea was to get the automatically generated ticklabels and use them to do the job:

labels = [label.get_text() for label in ax.get_xticklabels()]
ax.set_xticklabels(labels, alpha=alpha

The problem here is, the labels are empty due to the dynamic nature of matplotlib (see here).

So, is there an easy way to change the alpha of my ticklabels without knowing the text beforehand?

Upvotes: 2

Views: 905

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339705

It is probably not desirable to set the ticklabels themselves, if you want to change their color. The reason is that setting the labels via ax.set_ticklabels changes the formatter to a FixedFormatter; with this one would loose the automatic formatting behaviour.

Instead change the alpha of the text objects that later constitute the ticklabels. To this end plt.setp is a useful feature.

plt.setp(ax.get_xticklabels(), alpha=0.3)

The same can be achieved via

for t in ax.get_xticklabels():
    t.set_alpha(0.3)

Upvotes: 3

Related Questions