2015-07-26 18 views
37

che sto cercando di usare le mie proprie etichette per un barplot Seaborn con il seguente codice:assi etichetta sul Seaborn Barplot

import pandas as pd 
import seaborn as sns 

fake = pd.DataFrame({'cat': ['red', 'green', 'blue'], 'val': [1, 2, 3]}) 
fig = sns.barplot(x = 'val', y = 'cat', 
        data = fake, 
        color = 'black') 
fig.set_axis_labels('Colors', 'Values') 

enter image description here

Tuttavia, ottengo un errore che:

AttributeError: 'AxesSubplot' object has no attribute 'set_axis_labels' 

Cosa dà?

risposta

71

Il barplot di Seaborn restituisce un oggetto asse (non una figura). Ciò significa che è possibile effettuare le seguenti operazioni:

import pandas as pd 
import seaborn as sns 
import matplotlib.pyplot as plt 

fake = pd.DataFrame({'cat': ['red', 'green', 'blue'], 'val': [1, 2, 3]}) 
ax = sns.barplot(x = 'val', y = 'cat', 
       data = fake, 
       color = 'black') 
ax.set(xlabel='common xlabel', ylabel='common ylabel') 
plt.show() 
0

Si può evitare il AttributeError causata da set_axis_labels() metodo utilizzando il matplotlib.pyplot.xlabel e matplotlib.pyplot.ylabel.

matplotlib.pyplot.xlabel imposta l'etichetta asse x mentre il matplotlib.pyplot.xlabel imposta l'etichetta asse y dell'asse corrente. codice

Soluzione:

import pandas as pd 
import seaborn as sns 
import matplotlib.pyplot as plt 

fake = pd.DataFrame({'cat': ['red', 'green', 'blue'], 'val': [1, 2, 3]}) 
fig = sns.barplot(x = 'val', y = 'cat', data = fake, color = 'black') 
plt.xlabel("Colors") 
plt.ylabel("Values") 
plt.title("Colors vs Values") # You can comment this line out if you don't need title 
plt.show(fig) 

figura uscita:

enter image description here

Problemi correlati