deeenes - 6 months ago 87

Python Question

I am trying to plot simple barcharts with

`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')

To order the bars in a custom way, there is the

`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()))

However, if

`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])

Then I get the error

`AttributeError: 'bool' object has no attribute 'sum'`

Answer

`x_order`

should be a list of the labels, not the list of indexes. In other words, in the latter case you would just want `x_order=['a', 'b', 'c']`

.

Source (Stackoverflow)