Seaborn barplot with x_order: AttributeError: 'bool' object has no attribute 'sum'
Seaborn barplot with x_order: AttributeError: 'bool' object has no attribute 'sum'
我正在尝试使用 seaborn.barplot()
绘制简单的条形图。在最简单的情况下它运行良好,x
可以通过字符串向量或数字:
import numpy as np
import seaborn as sns
import matplotplib.pyplot as plt
fig, ax = plt.subplots()
ax = sns.barplot(np.array(['a','b','c']), y = np.array([1,2,3]))
fig.tight_layout()
fig.savefig('test.pdf')
要以自定义方式对条形进行排序,有 x_order
参数,它是一个带有索引的类似列表的对象。如果x
本身是数字,也能正常工作:
x = np.array([2, 0, 1])
y = np.array([3, 4, 2])
sns.barplot(x, y = y, x_order = list(x.argsort()))
但是,如果 x
不是数字,它会报错,即使我尝试按其他数字向量排序,或按字符串向量本身排序:
x = np.array(['b', 'c', 'a'])
y = np.array([3, 4, 2])
sns.barplot(x, y = y, x_order = list(x.argsort()))
sns.barplot(x, y = y, x_order = [2, 0, 1])
然后我得到错误 AttributeError: 'bool' object has no attribute 'sum'
。我对此了解不多,我想知道如何正确地进行这种简单的排序。
x_order
应该是标签列表,而不是索引列表。换句话说,在后一种情况下,您只需要 x_order=['a', 'b', 'c']
.
我正在尝试使用 seaborn.barplot()
绘制简单的条形图。在最简单的情况下它运行良好,x
可以通过字符串向量或数字:
import numpy as np
import seaborn as sns
import matplotplib.pyplot as plt
fig, ax = plt.subplots()
ax = sns.barplot(np.array(['a','b','c']), y = np.array([1,2,3]))
fig.tight_layout()
fig.savefig('test.pdf')
要以自定义方式对条形进行排序,有 x_order
参数,它是一个带有索引的类似列表的对象。如果x
本身是数字,也能正常工作:
x = np.array([2, 0, 1])
y = np.array([3, 4, 2])
sns.barplot(x, y = y, x_order = list(x.argsort()))
但是,如果 x
不是数字,它会报错,即使我尝试按其他数字向量排序,或按字符串向量本身排序:
x = np.array(['b', 'c', 'a'])
y = np.array([3, 4, 2])
sns.barplot(x, y = y, x_order = list(x.argsort()))
sns.barplot(x, y = y, x_order = [2, 0, 1])
然后我得到错误 AttributeError: 'bool' object has no attribute 'sum'
。我对此了解不多,我想知道如何正确地进行这种简单的排序。
x_order
应该是标签列表,而不是索引列表。换句话说,在后一种情况下,您只需要 x_order=['a', 'b', 'c']
.