Reputation: 495
I have a Pandas DataFrame
, and I groupby
a column, call .size
and then call .plot.bar
I want to reorder the bars by some columns in the original dataframe, but passing them in sort_columns
for bar()
is not working:
nodes.groupby("Name").size().plot.bar(sort_columns="Class")
Presorting the columns in the dataframe also has no effect - how can you order the columns in a Pandas bar chart?
Upvotes: 0
Views: 3728
Reputation: 31
I have created a random dataframe to help visualize the data.
#creating random data
data = {'a' : np.random.randint(0, 10, size = 10),
'b' : np.random.randint(0, 10, size = 10),
'c' : np.random.randint(0, 10, size = 10),
'd' : np.random.randint(0, 10, size = 10),
'e' : np.random.randint(0, 10, size = 10)}
df = pd.DataFrame(data)
# Custom sorting of columns
df.iloc[0][['a', 'c', 'd', 'b', 'e']].T.plot.bar()
[]
# Sorting columns in acsending or decending order
df.iloc[0].sort_values(ascending = False).T.plot.bar()
[]
Note: The parameter column_sort sorts the columns by their names not their values.
Upvotes: 2