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')
但是,我得到一个错误:
AttributeError:'AxesSubplot' object has no attribute 'set_axis_labels'
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)
One can avoid the AttributeError brought about by set_axis_labels() method by using the matplotlib.pyplot.xlabel and matplotlib.pyplot.ylabel.
matplotlib.pyplot.xlabel sets the x-axis label while the matplotlib.pyplot.ylabel sets the y-axis label of the current axis.
Solution code:
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)