在seaborn factorplot中旋转标签文本

我有一个简单的因素图

import seaborn as sns g = sns.factorplot("name", "miss_ratio", "policy", dodge=.2, linestyles=["none", "none", "none", "none"], data=df[df["level"] == 2]) 

在这里输入图像说明

问题是x标签全部一起运行,使得它们不可读。 你如何旋转文本,使标签可读?

阿曼是正确的,你可以使用正常的matplotlib命令,但这也是内置到FacetGrid

 import seaborn as sns planets = sns.load_dataset("planets") g = sns.factorplot("year", data=planets, aspect=1.5, kind="count", color="b") g.set_xticklabels(rotation=30) 

在这里输入图像说明

有一些评论和另一个答案,声称这“不起作用”,但是,任何人都可以运行在这里写的代码,看看它的工作。 另一个答案没有提供一个可重复使用的例子,这个例子很难解决,但是我的猜测是人们试图把这个解决scheme应用到返回Axes对象而不是Facet Grid的函数的输出中。 这些是不同的事情, Axes.set_xticklabels()方法确实需要一个标签列表,不能简单地改变Axes上现有标签的属性。 教训是,注意你正在使用什么样的对象是很重要的。

我对@mwaskorn的回答有一个问题,那就是

 g.set_xticklabels(rotation=30) 

失败,因为这也需要标签。 比@Aman的答案稍微简单一点就是添加

 plt.xticks(rotation=45) 

这仍然是一个matplotlib对象。 尝试这个:

 # <your code here> locs, labels = plt.xticks() plt.setp(labels, rotation=45) 

如果有人想知道如何为clustermap CorrGrids(给定的seaborn示例的一部分):

 import seaborn as sns import matplotlib.pyplot as plt sns.set(context="paper", font="monospace") # Load the datset of correlations between cortical brain networks df = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0) corrmat = df.corr() # Set up the matplotlib figure f, ax = plt.subplots(figsize=(12, 9)) # Draw the heatmap using seaborn g=sns.clustermap(corrmat, vmax=.8, square=True) rotation = 90 for i, ax in enumerate(g.fig.axes): ## getting all axes of the fig object ax.set_xticklabels(ax.get_xticklabels(), rotation = rotation) g.fig.show() 

什么对我有效:

 planets = sns.load_dataset("planets") g = sns.factorplot("year", data=planets, aspect=1.5,kind="count", color="b") g.set_xticklabels(labels = planets["year"].value_counts().index.tolist(),rotation=30) 

对于seaborn.heatmap ,你可以旋转这些使用(基于@阿曼的答案 )

 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