2014-11-19 13 views

risposta

16

Sulla base di @ user308827 risposta: è possibile utilizzare legend=False in factorplot e specificare la leggenda attraverso matplotlib:

import seaborn as sns 
import matplotlib.pyplot as plt 
sns.set(style="whitegrid") 

titanic = sns.load_dataset("titanic") 

g = sns.factorplot("class", "survived", "sex", 
        data=titanic, kind="bar", 
        size=6, palette="muted", 
        legend=False) 
g.despine(left=True) 
plt.legend(loc='upper left') 
g.set_ylabels("survival probability") 
+0

Ha funzionato, grazie! Piccola domanda: come fa il "plt" globale a sapere in quale cifra aggiungere la legenda? Ho molte figure aperte allo stesso tempo. – user124114

+5

'plt' agisce sugli assi correnti. Per ottenere gli assi da un 'FacetGrid' usa la fig. Ad esempio: 'g.fig.get_axes() [0] .legend (loc = 'in basso a sinistra')' – Jules

9

Modificare l'esempio da http://web.stanford.edu/~mwaskom/software/seaborn/examples/factorplot_bars.html

È possibile utilizzare legend_out = False

import seaborn as sns 
sns.set(style="whitegrid") 

titanic = sns.load_dataset("titanic") 

g = sns.factorplot("class", "survived", "sex", 
        data=titanic, kind="bar", 
        size=6, palette="muted", 
        legend_out=False) 
g.despine(left=True) 
g.set_ylabels("survival probability") 

enter image description here

+0

Dove inserisco questo 'loc', esattamente? – user124114

+0

ok, ho modificato la mia risposta. using legend_out = False mi dà un risultato migliore. – user308827

+0

Questo aiuta. Aspetterò un po 'per vedere se qualcuno sa come posizionare la legenda esplicitamente. In caso contrario, accetterò questa risposta. Grazie! – user124114

0

Questo è come sono stato in grado di spostare la legenda in un determinato luogo all'interno della trama e cambiare l'aspetto e dimensioni del terreno:

import matplotlib 
matplotlib.use('Agg') 
import matplotlib.pyplot as plt 
matplotlib.style.use('ggplot') 
import seaborn as sns 
sns.set(style="ticks") 

figure_name = 'rater_violinplot.png' 
figure_output_path = output_path + figure_name 

viol_plot = sns.factorplot(x="Rater", 
         y="Confidence", 
         hue="Event Type", 
         data=combo_df, 
         palette="colorblind", 
         kind='violin', 
         size = 10, 
         aspect = 1.5, 
         legend=False) 

viol_plot.ax.legend(loc=2) 
viol_plot.fig.savefig(figure_output_path) 

Legend location changed

Questo ha funzionato per me di cambiare le dimensioni e aspetto della trama e spostare la legenda al di fuori dell'area del tracciato.

import matplotlib 
matplotlib.use('Agg') 
import matplotlib.pyplot as plt 
matplotlib.style.use('ggplot') 
import seaborn as sns 
sns.set(style="ticks") 


figure_name = 'rater_violinplot.png' 
figure_output_path = output_path + figure_name 

viol_plot = sns.factorplot(x="Rater", 
         y="Confidence", 
         hue="Event Type", 
         data=combo_df, 
         palette="colorblind", 
         kind='violin', 
         size = 10, 
         aspect = 1.5, 
         legend_out=True) 

viol_plot.fig.savefig(figure_output_path) 

violin plot with changed size, aspect and legend located outside

ho capito questo dalla risposta di mwaskom here e la risposta di Fernando Hernandez here.

Problemi correlati