#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read min read

Interactive Bar Charts

Learn to create interactive bar charts with Plotly

Interactive Bar Charts

Basic Bar Chart

code.py
import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    'Product': ['A', 'B', 'C', 'D'],
    'Sales': [100, 150, 80, 120]
})

fig = px.bar(df, x='Product', y='Sales', title='Sales by Product')
fig.show()

Horizontal Bar Chart

code.py
fig = px.bar(df, x='Sales', y='Product', orientation='h')
fig.show()

Color by Category

code.py
df['Region'] = ['East', 'West', 'East', 'West']

fig = px.bar(df, x='Product', y='Sales', color='Region')
fig.show()

Grouped Bar Chart

code.py
df = pd.DataFrame({
    'Product': ['A', 'B', 'C', 'A', 'B', 'C'],
    'Sales': [100, 150, 80, 120, 140, 90],
    'Year': ['2023', '2023', '2023', '2024', '2024', '2024']
})

fig = px.bar(df, x='Product', y='Sales', color='Year', barmode='group')
fig.show()

Stacked Bar Chart

code.py
fig = px.bar(df, x='Product', y='Sales', color='Year', barmode='stack')
fig.show()

Add Text on Bars

code.py
fig = px.bar(df, x='Product', y='Sales', text='Sales')
fig.show()

Custom Text Position

code.py
fig = px.bar(df, x='Product', y='Sales', text='Sales')
fig.update_traces(textposition='outside')
fig.show()

Positions: 'inside', 'outside', 'auto', 'none'

Sort Bars

code.py
# Sort by value
df_sorted = df.sort_values('Sales', ascending=False)
fig = px.bar(df_sorted, x='Product', y='Sales')
fig.show()

Custom Colors

code.py
fig = px.bar(df, x='Product', y='Sales',
             color='Product',
             color_discrete_sequence=['red', 'blue', 'green', 'orange'])
fig.show()

Percentage Bar Chart

code.py
fig = px.bar(df, x='Product', y='Sales', color='Year',
             barmode='stack', barnorm='percent')
fig.show()

Shows percentages instead of values!

Faceted Bar Charts

code.py
df['Region'] = ['East', 'East', 'West', 'West', 'East', 'West']

fig = px.bar(df, x='Product', y='Sales', color='Year',
             facet_col='Region', barmode='group')
fig.show()

Complete Example

code.py
import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    'Department': ['Sales', 'IT', 'HR', 'Marketing'],
    'Q1': [100, 80, 50, 70],
    'Q2': [120, 85, 55, 80],
    'Q3': [115, 90, 60, 75],
    'Q4': [140, 95, 65, 90]
})

# Reshape for Plotly
df_long = df.melt(id_vars='Department', var_name='Quarter', value_name='Revenue')

fig = px.bar(df_long, x='Department', y='Revenue',
             color='Quarter', barmode='group',
             title='Quarterly Revenue by Department',
             text='Revenue')

fig.update_traces(textposition='outside')
fig.show()

Key Points

  • px.bar() for bar charts
  • orientation='h' for horizontal
  • barmode='group' side by side
  • barmode='stack' stacked
  • text shows values on bars
  • barnorm='percent' for percentages
  • Use melt() to reshape data

What's Next?

Learn to create 3D visualizations with Plotly.

SkillsetMaster - AI, Web Development & Data Analytics Courses