简体   繁体   English

调用 set_xticklabels 时打印标签

[英]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 .使用set_xticklabels设置 x 轴标签的旋转时,将打印所有标签。

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截屏

在此处输入图片说明

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.我不知道如何修复它并用set_xticklabels搜索但没有答案,所以我将标签单独放在一个列表中并在set_xticklabels但仍然没有运气。

Thanks in advance :)提前致谢 :)

This is not really a mistake, but rather, a characteristic of matplotlib , which seaborn uses.这并不是一个真正的错误,而是seaborn使用的matplotlib一个特征。 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.在这种情况下, set_xticklabels修改使用Text对象绘制的刻度标签 It is those Text objects, collected in a list , that are returned.返回的是那些收集在list Text对象。

What you perceive as the labels being "printed" is simply your Jupyter notebook representing that list as text.您认为“打印”的标签只是您的 Jupyter 笔记本,将该list表示为文本。

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.另一种选择是简单地将pass语句附加到您的代码中。 Since Jupyter will render the return value of the last statement in the cell, and pass returns nothing, you will not get any output.由于 Jupyter 将呈现单元格中最后一条语句的返回值,而pass返回任何内容,因此您不会得到任何输出。

if you are using jupyter notebook just如果您正在使用 jupyter notebook

ax.set_xticklabels(...) ; ax.set_xticklabels(...) ; <- line ends by a semicolon. <- 行以分号结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM