旋转Seaborn箱图中的xtick标签

旋转Seaborn箱图中的xtick标签

本文介绍了旋转Seaborn箱图中的xtick标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与 2014 年的问题基本相同的问题(参见

我想将 x 标签旋转 30 度.因此,我使用 g.set_xticklabels(rotation = 30).但是,我收到以下错误:

set_xticklabels() 缺少 1 个必需的位置参数:'labels'

我不知道如何将 matplotlib labels 参数传递给seaborns sns.boxplot .有任何想法吗?

解决方案

您链接到的问题使用 factorplot .因子图返回它自己的类,该类有一个名为 set_xticklabels(rotation) 的方法.这与matplotlib Axes set_xticklabels 方法不同.

在链接问题的答案中,您还可以使用其他选项

ax = sns.boxplot(x='categories', y='oxygen',hue='target', data=df)ax.set_xticklabels(ax.get_xticklabels(),rotation = 30)

ax = sns.boxplot(x='categories', y='oxygen',hue='target', data=df)plt.setp(ax.get_xticklabels(),旋转= 45)

I have a question that is basically the same as a question back from 2014 (see here). However, my script still throws an error.

Here is what I do: I have a pandas dataframe with a few columns. I plot a simple boxplot comparison.

g = sns.boxplot(x='categories', y='oxygen', hue='target', data=df)
g.set_xticklabels(rotation=30)

The graph looks like this:

I'd like to rotate the x-labels by 30 degrees. Hence I use g.set_xticklabels(rotation=30). However, I get the following error:

set_xticklabels() missing 1 required positional argument: 'labels'

I don't know how to pass the matplotlib labels argument to seaborns sns.boxplot. Any ideas?

解决方案

The question you link to uses a factorplot. A factorplot returns its own class which has a method called set_xticklabels(rotation). This is different from the set_xticklabels method of the matplotlib Axes.

In the linked question's answers there are also other options which you may use

ax = sns.boxplot(x='categories', y='oxygen', hue='target', data=df)
ax.set_xticklabels(ax.get_xticklabels(),rotation=30)

or

ax = sns.boxplot(x='categories', y='oxygen', hue='target', data=df)
plt.setp(ax.get_xticklabels(), rotation=45)

这篇关于旋转Seaborn箱图中的xtick标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:17