如何在 Seaborn PairGrid 中*旋转*标签?

How to *Rotate* labels in a Seaborn PairGrid?

我的数据框中的列名称很长,所以当我制作成对图时,标签彼此重叠。我想将我的标签旋转 90 度,这样它们就不会发生碰撞。我尝试查找在线和文档,但找不到解决方案。这是我写的东西和错误消息:

plt.figure(figsize=(10,10))
g = sn.pairplot(df, kind="scatter")
g.set_xticklabels(g.get_xticklabels(), rotation=90)
g.set_yticklabels(g.get_yticklabels(), rotation=90)
AttributeError: 'PairGrid' object has no attribute 'set_xticklabels'

如何在 Seaborn PairGrid 中旋转标签(x 和 y)?

注:抱歉,我的wifi无法上传图片以供参考

您可以直接使用 PairPlot 返回的 PairGrid 对象的 axes 成员遍历轴。像这样

for ax in g.axes.flatten():
    ax.tick_params(rotation = 90)

应该可以解决问题

感谢 William 的回答,我现在知道要寻找什么来解决我的问题了!

下面是我的做法。

g = sn.pairplot(dfsub.sample(50), kind="scatter", hue=target)
for ax in g.axes.flatten():
    # rotate x axis labels
    ax.set_xlabel(ax.get_xlabel(), rotation = 90)
    # rotate y axis labels
    ax.set_ylabel(ax.get_ylabel(), rotation = 0)
    # set y labels alignment
    ax.yaxis.get_label().set_horizontalalignment('right')

添加到@techtana 的回答中。如果在绘制 pairplot 时设置 cornor=True,则需要跳过 NoneTypeaxes:

g = sns.pairplot(df, diag_kind='kde', corner=True)
for ax in g.axes.flatten():
    if ax:
        # rotate x axis labels
        ax.set_xlabel(ax.get_xlabel(), rotation = -30)
        # rotate y axis labels
        ax.set_ylabel(ax.get_ylabel(), rotation = 0)
        # set y labels alignment
        ax.yaxis.get_label().set_horizontalalignment('right')