queuer
queuer

Reputation: 25

How to generate a group bar plot with different numbers of bars in each group?

I know how to generate a common group bar chart with matplotlib like this: A Grouped bar chart from matplotlib

It has 2 bars in each group.

But how can I generate a group bar plot with different numbers of bars in each group like 2 bars in Group1 but 3 bars in Group2?

Thanks.

Upvotes: 0

Views: 969

Answers (1)

JohanC
JohanC

Reputation: 80509

You can use catplot, for example as follows:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({'group': ['group 1', 'group 1', 'group 2', 'group 2', 'group 2'],
                   'bar': ['bar 1', 'bar 2', 'bar 1', 'bar 2', 'bar 3'],
                   'value': [1, 2, 3, 4, 5]})
g = sns.catplot(kind='bar', data=df, col='group', x='bar', y='value',
                hue='bar', palette='rocket', dodge=False, sharex=False)
plt.tight_layout()
plt.show()

seaborn barplots with different numbers of bars

Upvotes: 2

Related Questions