2014-10-24 12 views
47

Ho una semplice factorplotruotare il testo dell'etichetta in 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

Il problema è che le etichette x tutti corrono insieme, rendendoli illeggibili. Come ruoti il ​​testo in modo che le etichette siano leggibili?

+8

'import matplotlib.pylab as plt' . 'plt.xticks (rotation = 45)' – rafaelvalle

+0

@rafaelvalle questa è l'unica cosa che funziona per me ora. Grazie! – szeitlin

risposta

61

Aman è corretto che è possibile utilizzare normali comandi matplotlib, ma questo è costruito anche nella 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

Ci sono alcuni commenti e un'altra risposta rivendicando questo "non funziona", tuttavia, chiunque può eseguire il codice come scritto qui e vedere che funziona. L'altra risposta non fornisce un esempio riproducibile di ciò che non funziona, rendendo molto difficile l'indirizzamento, ma la mia ipotesi è che le persone stiano cercando di applicare questa soluzione all'output di funzioni che restituiscono an Axes object anziché Facet Grid. Si tratta di cose diverse e il metodo Axes.set_xticklabels() richiede effettivamente un elenco di etichette e non può semplicemente modificare le proprietà delle etichette esistenti su Axes. La lezione è che è importante prestare attenzione al tipo di oggetti con cui si lavora.

+1

Nizza; questo è molto più facile per gli occhi. – Aman

+0

Ottengo un errore: IndexError: troppi indici. Il mio codice! g = sns.factorplot ("month_date", "num_proposals", "account", sorted_data, col = "account", col_wrap = 3, sharex = False); g.set_xticklabels (rotation = 30) – yoshiserry

+0

cosa succede se tracciai 'DataFrame' con' sns.pairplot'? come lanciarlo su ogni grafico? – soupault

21

Questo è ancora un oggetto matplotlib. Prova questo:

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

ho avuto un problema con la risposta @mwaskorn, vale a dire che

g.set_xticklabels(rotation=30) 

fallisce, perché questo richiede anche le etichette. Un po 'più facile che la risposta da @Aman è quello di aggiungere appena

plt.xticks(rotation=45) 
+20

Puoi anche ottenere le etichette in questo modo 'g.set_xticklabels (g. get_xticklabels(), rotation = 30) '. Assegnarlo a una variabile se si desidera sopprimere l'output. –

1

cosa ha funzionato per me:

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

Se qualcuno si chiede come questo per CorrGrids clustermap (parte di un dato esempio 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() 
1

Per un seaborn.heatmap, è possibile ruotare questi usando (in base a @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 
Problemi correlati