2015-07-26 25 views
37

Próbuję użyć własnych etykiet na barplot Seaborn z następującego kodu:osie etykieta na 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

Jednakże pojawia się błąd, że:

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

Co daje?

Odpowiedz

71

Barbet Seaborn zwraca obiekt osi (nie figurę). Oznacza to, że można wykonać następujące czynności:

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

Można uniknąć AttributeError spowodowanej przez set_axis_labels() metody za pomocą matplotlib.pyplot.xlabel i matplotlib.pyplot.ylabel.

matplotlib.pyplot.xlabel przedstawia etykietę wokół osi x, natomiast matplotlib.pyplot.xlabel przedstawia etykietę oś y obecnego osi. Kod

Rozwiązanie:

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) 

postać wyjściowa:

enter image description here