python-3.x - 在seaborn箱线图中旋转xtick标签?

标签 python-3.x pandas matplotlib seaborn

我有一个问题与 2014 年的问题基本相同(请参阅 here )。但是,我的脚本仍然抛出错误。

这就是我所做的:我有一个包含几列的 pandas 数据框。我绘制了一个简单的箱线图比较。

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

图表如下所示:

enter image description here

我想将 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(), rotation=45)

关于python-3.x - 在seaborn箱线图中旋转xtick标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44954123/

相关文章:

python-3.x - 设置了SPARK_HOME env变量,但是Jupyter Notebook没有看到它。 ( window )

python - Pandas Dataframe 数据是相同的还是新的?

python - 为什么 pandas read_csv 发出此警告? (元素比较失败)

python - Matplotlib 副标题打印在旧副标题上

python - 比较两个数据帧并出现错误

python-3.x - 在使用 ipdb ResourceWarning 进行 unitest 时调试 python 代码

python - 添加倾斜( float )轴以与标准轴叠加

python - 带有 Pandas 或 python 的 Sankey 条形图图表

python-3.x - 在 Heroku 上使用 Django 运行 PostGIS

python - 使用固定宽度拆分字符串类型的 Pandas 列(类似于具有固定宽度的 Excel 文本到列功能)