python - Seaborn barplot with x_order: AttributeError: 'bool' object has no attribute 'sum' -
i trying plot simple barcharts seaborn.barplot()
. in simplest case works well, x
can vector of strings, or numbers:
import numpy np import seaborn sns import matplotplib.pyplot 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')
to order bars in custom way, there x_order
argument, list-like object indexes. if x
numeric, works well:
x = np.array([2, 0, 1]) y = np.array([3, 4, 2]) sns.barplot(x, y = y, x_order = list(x.argsort()))
however, if x
not numeric, gives error, if try order other numeric vector, or ordering string vector itself:
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])
then error attributeerror: 'bool' object has no attribute 'sum'
. haven't found this, , wondering how simple ordering properly.
x_order
should list of labels, not list of indexes. in other words, in latter case want x_order=['a', 'b', 'c']
.
Comments
Post a Comment