How to Group Bar Charts in Python-Plotly?

Visualizing data is a critical step in understanding and interpreting complex data. Among numerous chart types, the bar chart remains a versatile and popular choice for representing categorical data. Using Python and Plotly, we can create interactive grouped bar charts that help compare multiple series of data across the same categories.

Grouped bar charts are particularly useful when comparing multiple data series side by side, making it easy to identify patterns, correlations, and contrasts in your data.

Syntax

The standard syntax for creating a grouped bar chart using Plotly Express is ?

plotly.express.bar(data_frame, x, y, color, barmode='group', ...)

Parameters:

  • data_frame ? The DataFrame containing your data source

  • x ? Column representing x-axis values (categories)

  • y ? Column representing y-axis values (numeric data)

  • color ? Column used to group and color the bars

  • barmode ? Set to 'group' for side-by-side bars

Creating a Grouped Bar Chart from Scratch

Let's create a grouped bar chart showing sales data for three products across four quarters ?

import plotly.express as px
import pandas as pd

# Create sample data
data = {
    'Quarters': ['Q1', 'Q2', 'Q3', 'Q4'],
    'Product A': [200, 150, 100, 180],
    'Product B': [220, 130, 90, 150],
    'Product C': [210, 160, 130, 170]
}

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# Convert from wide to long format
df_melt = df.melt(id_vars='Quarters', var_name='Products', value_name='Sales')
print("\nMelted DataFrame:")
print(df_melt.head())

# Create grouped bar chart
fig = px.bar(df_melt, x='Quarters', y='Sales', color='Products', barmode='group')
fig.update_layout(title='Quarterly Sales by Product')
fig.show()
Original DataFrame:
  Quarters  Product A  Product B  Product C
0       Q1        200        220        210
1       Q2        150        130        160
2       Q3        100         90        130
3       Q4        180        150        170

Melted DataFrame:
  Quarters   Products  Sales
0       Q1  Product A    200
1       Q2  Product A    150
2       Q3  Product A    100
3       Q4  Product A    180
4       Q1  Product B    220

Using Built-in Sample Data

Plotly includes sample datasets that we can use for practice ?

import plotly.express as px

# Load the tips dataset
df = px.data.tips()
print("Tips dataset shape:", df.shape)
print("\nFirst few rows:")
print(df.head())

# Create grouped bar chart
fig = px.bar(df, x='day', y='total_bill', color='sex', barmode='group')
fig.update_layout(
    title='Average Total Bill by Day and Gender',
    xaxis_title='Day of Week',
    yaxis_title='Total Bill ($)'
)
fig.show()
Tips dataset shape: (244, 7)

First few rows:
   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4

Customizing Grouped Bar Charts

You can customize colors, labels, and layout for better presentation ?

import plotly.express as px
import pandas as pd

# Create sample data
regions_data = {
    'Region': ['North', 'South', 'East', 'West'],
    'Electronics': [45000, 38000, 52000, 41000],
    'Clothing': [32000, 45000, 28000, 39000],
    'Books': [15000, 18000, 22000, 16000]
}

df = pd.DataFrame(regions_data)
df_melt = df.melt(id_vars='Region', var_name='Category', value_name='Sales')

# Create customized grouped bar chart
fig = px.bar(df_melt, x='Region', y='Sales', color='Category', 
             barmode='group',
             color_discrete_map={
                 'Electronics': '#FF6B6B',
                 'Clothing': '#4ECDC4', 
                 'Books': '#45B7D1'
             })

fig.update_layout(
    title='Regional Sales by Product Category',
    xaxis_title='Region',
    yaxis_title='Sales ($)',
    legend_title='Product Category',
    font=dict(size=12)
)

fig.show()
Chart displays with custom colors:
- Electronics bars in red (#FF6B6B)
- Clothing bars in teal (#4ECDC4) 
- Books bars in blue (#45B7D1)

Comparison of Bar Chart Modes

Bar Mode Description Best For
group Bars placed side by side Comparing values across categories
stack Bars stacked on top of each other Showing parts of a whole
overlay Bars overlapping Comparing distributions

Conclusion

Grouped bar charts in Plotly provide an excellent way to compare multiple data series across categories. Use px.bar() with barmode='group' and the color parameter to create these visualizations effectively. The key is transforming your data into long format using melt() for proper grouping.

Updated on: 2026-03-27T10:17:11+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements