All labels are getting printed when setting the rotation for the x-axis label using set_xticklabels.


winner_freq = pd.DataFrame(player_match.winner.value_counts().reset_index())
winner_freq_plot = sns.barplot(x='index', y='winner', data=winner_freq)
winner_freq_plot.set_xticklabels(winner_freq_plot.get_xticklabels(), rotation=90)


enter image description here

Fix that I tried

I don't have any idea how to fix it and googled but no answer, so I took the labels separately in a list and feed inside the set_xticklabels but still no luck.

Thanks in advance :)

1 Answers

Marcus Lim On Best Solutions

This is not really a mistake, but rather, a characteristic of matplotlib, which seaborn uses. Most of its functions return values that in some way represent the computations that they have performed.

In this case, set_xticklabels modifies the tick labels, which are drawn with Text objects. It is those Text objects, collected in a list, that are returned.

What you perceive as the labels being "printed" is simply your Jupyter notebook representing that list as text.

If you do not wish to see this, you can assign the result to a throwaway variable, like this:

_ = winner_freq_plot.set_xticklabels(winner_freq_plot.get_xticklabels(), rotation=90)

That said, note that _ normally binds to the last return value, and you will override that by doing so.

An alternative is to simply append a pass statement to your code. Since Jupyter will render the return value of the last statement in the cell, and pass returns nothing, you will not get any output.