Метки топоров на Seaborn Barplot
Я пытаюсь использовать свои собственные ярлыки для барбота Seaborn со следующим кодом:
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]()
Однако я получаю сообщение об ошибке:
AttributeError: 'AxesSubplot' object has no attribute 'set_axis_labels'
Что дает?
Ответы
Ответ 1
Морской барплот возвращает ось-объект (а не фигуру). Это означает, что вы можете сделать следующее:
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()
Ответ 2
Можно избежать set_axis_labels()
AttributeError
вызванной методом set_axis_labels()
, используя matplotlib.pyplot.xlabel
и matplotlib.pyplot.ylabel
.
matplotlib.pyplot.xlabel
устанавливает метку оси x, а matplotlib.pyplot.ylabel
устанавливает метку оси y текущей оси.
Код решения:
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)
Выходной рисунок:
![enter image description here]()
Ответ 3
Вы также можете установить заголовок своего графика, добавив параметр title следующим образом
ax.set(xlabel='common xlabel', ylabel='common ylabel', title='some title')