Seaborn 条形图上的标签轴
Label axes on Seaborn Barplot
我正在尝试使用我自己的标签为 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')
但是,我得到一个错误:
AttributeError: 'AxesSubplot' object has no attribute 'set_axis_labels'
什么给了?
Seaborn 的条形图 returns 轴对象(不是图形)。这意味着您可以执行以下操作:
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()
可以通过matplotlib.pyplot.xlabel
和[=14来避免set_axis_labels()
方法带来的AttributeError
=].
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)
输出图:
您还可以通过添加标题参数来设置图表的标题,如下所示
ax.set(xlabel='common xlabel', ylabel='common ylabel', title='some title')
另一种方法是直接在 seaborn 绘图对象中访问该方法。
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("Colors")
ax.set_ylabel("Values")
ax.set_yticklabels(['Red', 'Green', 'Blue'])
ax.set_title("Colors vs Values")
产生:
我正在尝试使用我自己的标签为 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')
但是,我得到一个错误:
AttributeError: 'AxesSubplot' object has no attribute 'set_axis_labels'
什么给了?
Seaborn 的条形图 returns 轴对象(不是图形)。这意味着您可以执行以下操作:
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()
可以通过matplotlib.pyplot.xlabel
和[=14来避免set_axis_labels()
方法带来的AttributeError
=].
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)
输出图:
您还可以通过添加标题参数来设置图表的标题,如下所示
ax.set(xlabel='common xlabel', ylabel='common ylabel', title='some title')
另一种方法是直接在 seaborn 绘图对象中访问该方法。
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("Colors")
ax.set_ylabel("Values")
ax.set_yticklabels(['Red', 'Green', 'Blue'])
ax.set_title("Colors vs Values")
产生: