Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

labels are printed when set_xticklabels called

Issue:

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

Code

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)

Screenshot

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

like image 481
Thinker Avatar asked May 10 '19 10:05

Thinker


2 Answers

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, use a semicolon at the end of the last statement in a cell to prevent its output.

Alternatively, 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.

Another 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.

like image 118
gmds Avatar answered Nov 06 '22 22:11

gmds


if you are using jupyter notebook just

ax.set_xticklabels(...); <- line ends by a semicolon.

like image 1
Rodrigo Barbosa Avatar answered Nov 06 '22 22:11

Rodrigo Barbosa