web-dev-qa-db-ja.com

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])

enter image description here

問題は、xラベルがすべて一緒に実行され、読み取り不能になることです。ラベルが読めるようにテキストをどのように回転させますか?

100
dan

Amanは通常の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)

enter image description here

この「機能しない」と主張するコメントと別の回答がありますが、誰でもここで書かれたコードを実行して、機能することを確認できます。他の答えは、機能していないものの再現可能な例を提供しておらず、対処するのが非常に困難ですが、私の推測では、このソリューションを anを返す関数の出力に適用しようとしていますFacet Gridの代わりにAxes object 。これらは異なるものであり、 Axes.set_xticklabels() メソッドは実際にラベルのリストを必要とし、Axesの既存のラベルのプロパティを単に変更することはできません。教訓は、どのような種類のオブジェクトを扱っているかに注意を払うことが重要だということです。

96
mwaskom

@mwaskornの回答に問題がありました。

g.set_xticklabels(rotation=30)

これもラベルが必要なため失敗します。 @Amanによる答えよりも少し簡単なのは、単に追加することです

plt.xticks(rotation=45)
182
Gijs

これは、まだmatplotlibオブジェクトです。これを試して:

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

誰かが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()
4
user5875384

seaborn.heatmap の場合、( @ Aman's answer に基づいて)を使用してこれらを回転できます

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
4
serv-inc

次のようにplt.setpを使用することもできます。

import matplotlib.pyplot as plt
import seaborn as sns

plot=sns.barplot(data=df,  x=" ", y=" ")
plt.setp(plot.get_xticklabels(), rotation=90)

ラベルを90度回転します。

2
Robvh