I am working with pandas and seaborn to generate a fairly large barplot. The x axis consists of multiple identifying numbers, while the y axis displays counts. I am trying to order the x axis and sort the barplot in descending order based on counts of a binary variable (which is shown with seaborn barplot’shue
feature.)
Currently, I have tried to pass a sorted dataframe into the by
parameter of barplot, as such:
count= df['binary_variable'].value_counts()
df['count'] = df['binary_variable'].map(count)
df.sort_values(by = 'count', ascending = False, inplace = True)
sns.barplot(data = df, x = 'ID', hue = 'binary_variable')
However, this seems to have no effect on the barplot. After inspecting the sorted dataframe using .head(), it appears the sorting mechanism works. However, it does not affect the barplot.
I have also tried to change the x axis into type str with df['unique_id'] = df['unique_id'].astype(str)
, which seemed to work in the case of a question posed here: Seaborn catplot Sort by Count column
However, this method does not work in this case.
Here is some dummy data that may help with this case:
ID | binary_variable
1 1
1 1
1 0
2 1
2 0
3 1
4 1
4 1
4 1
4 1
In this dummy case, I would want the bar with ID 4
to be leftmost as it has the highest count of 1 in the binary_variable
column.
Please advise.
7