2014-05-09 10 views
13

Come è possibile mettere la legenda al di fuori della trama?Come mettere la leggenda fuori dalla trama con i panda

import pandas as pd 
import matplotlib.pyplot as plt 
a = {'Test1': {1: 21867186, 4: 20145576, 10: 18018537}, 
    'Test2': {1: 23256313, 4: 21668216, 10: 19795367}} 

d = pd.DataFrame(a).T 
#print d 

f = plt.figure() 

plt.title('Title here!', color='black') 
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)) 
d.plot(kind='bar', ax=f.gca()) 
plt.show() 
+13

In realtà non è un duplicato completo ... la domanda è specifica per Pandas. La risposta seguente mostra come assegnare un asse ad una chiamata di funzione plot da 'pandas.DataFrame.plot' che rende possibile applicare i perfezionamenti' matplotlib.pyplot'. – slushy

+0

Sì, questo non è un duplicato ed è fastidioso che sia contrassegnato come tale. – ferrouswheel

risposta

22

penso che è necessario chiamare plot prima di aggiungere il chiamante legend.

import pandas as pd 
import matplotlib.pyplot as plt 
a = {'Test1': {1: 21867186, 4: 20145576, 10: 18018537}, 
    'Test2': {1: 23256313, 4: 21668216, 10: 19795367}} 

d = pd.DataFrame(a).T 
#print d 

f = plt.figure() 

plt.title('Title here!', color='black') 
d.plot(kind='bar', ax=f.gca()) 
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5)) 
plt.show() 
+1

Tuttavia, metà della legenda ora non è in figura? Come è possibile risolverlo? – user977828

+0

È davvero una domanda separata, ma si potrebbe provare 'f.subplots_adjust (right = 0.8)'. – Phil

+3

Se vuoi solo spostare la legenda, puoi sfruttare il fatto che '' .plot'' restituisce un asse matplotlib e aggiungi '' .legend (bbox_to_anchor = (1,1)) '' alla fine del tuo trama del codice –

Problemi correlati