0
votes

How do you plot a bar chart without aggregation? I have two columns, one contains values and the other is categorical, but I want to plot each row individually, without aggregation.

By default, sns.barplot(x = "col1", y = "col2", data = df) will aggregate by taking the mean of the values for each category in col1.

How do I simply just plot a bar for each row in my dataframe with no aggregation?

1
It's not clear what you want. If you want to plot col2 against col1, how do you handle repeated labels in col1? - Quang Hoang

1 Answers

1
votes

In case 'col1' only contains unique labels, you immediately get your result with sns.barplot(x='col1', y='col2', data=df). In case there are repeated labels, you can use the index as x and afterwards change the ticks:

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

df = pd.DataFrame({'col1': list('ababab'), 'col2': np.random.randint(10, 20, 6)})
ax = sns.barplot(x=df.index, y='col2', data=df)
ax.set_xticklabels(df['col1'])
ax.set_xlabel('col1')
plt.show()

example plot

PS: Similarly, a horizontal bar chart could be created as:

df = pd.DataFrame({'col1': list('ababab'), 'col2': np.random.randint(10, 20, 6)})
ax = sns.barplot(x='col2', y=df.index, data=df, orient='h')
ax.set_yticklabels(df['col1'])
ax.set_ylabel('col1')