Python 旋转seaborn条形图的刻度标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31859285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:40:21  来源:igfitidea点击:

Rotate tick labels for seaborn barplot

pythonmatplotlibseaborn

提问by Laurennmc

I am trying to display a chart with rotated x-axis labels, but the chart is not displaying.

我正在尝试显示带有旋转 x 轴标签的图表,但该图表未显示。

import seaborn as sns
%matplotlib inline

yellow='#FFB11E'
by_school=sns.barplot(x ='Organization Name',y ='Score',data = combined.sort('Organization Name'),color=yellow,ci=None)

At this point I can see the image, but after I set the xticklabel, I don't see the image anymore only an object reference. (I would post the image, but I don't enough reputation :()

此时我可以看到图像,但是在设置 xticklabel 后,我不再看到图像,只有对象引用。(我会发布图片,但我没有足够的声誉:()

by_school.set_xticklabels('Organization Name',rotation=45)

<matplotlib.axes._subplots.AxesSubplot at 0x3971a6a0>

A similar question is posted here: Rotate label text in seaborn factorplotbut the solution is not working.

这里发布了一个类似的问题:Rotate label text in seaborn factorplot但解决方案不起作用。

采纳答案by CT Zhu

You need a different method call, namely .set_rotationfor each ticklables. Since you already have the ticklabels, just change their rotations:

您需要一个不同的方法调用,即.set_rotation每个ticklables。由于您已经有了刻度标签,只需更改它们的旋转:

for item in by_school.get_xticklabels():
    item.set_rotation(45)

barplotreturns a matplotlib.axesobject (as of seaborn0.6.0), therefore you have to rotate the labels this way. In other cases, when the method returns a FacetGridobject, refer to Rotate label text in seaborn factorplot

barplot返回一个matplotlib.axes对象(从seaborn0.6.0 开始),因此您必须以这种方式旋转标签。其他情况,当方法返回FacetGrid对象时,参考在seaborn factorplot中旋转标签文本

回答by serv-inc

If you come here to rotate the labels for a seaborn.heatmap, the following should work (based on @Aman's answerat Rotate label text in seaborn factorplot)

如果您来这里旋转 a 的标签seaborn.heatmap,则以下内容应该有效(基于@Aman 在 Seaborn factorplot旋转标签文本的回答

pandas_frame = pd.DataFrame(data, index=names, columns=names)
heatmap = seaborn.heatmap(pandas_frame)
loc, labels = plt.xticks()
heatmap.set_xticklabels(labels, rotation=45)
heatmap.set_yticklabels(labels[::-1], rotation=45) # reversed order for y

回答by Amit Vikram Singh

Use the following code statement:

使用以下代码语句:

by_school.set_xticklabels(by_school.get_xticklabels(), rotation=90, horizontalalignment='right')

回答by Kairat Sharshenbaev

This worked for me:

这对我有用:

g.fig.autofmt_xdate()

回答by wordsforthewise

You can rotate seaborn xticks like so:

你可以像这样旋转seaborn xticks:

sns.barplot(x='Organization Name', y='Score', data=df)

plt.xticks(rotation=70)
plt.tight_layout()