Skip to content

Interactive Bar Charts

When to reach for a bar chart

Bars are the right shape when you’re comparing totals across separate categories — cities, products, days of the week — where each category stands on its own (no implied continuity between them, unlike a line chart). Bar length is one of the easiest visual encodings for people to compare accurately, which is why bar charts are the safest default for “which is bigger?” questions.

Basic bar chart

Interactive bar
import pandas as pd
import plotly.express as px
 
df = pd.DataFrame({
    "city": ["Pune", "Delhi", "Mumbai", "Bengaluru"],
    "sales": [120, 180, 90, 160],
})
 
fig = px.bar(df, x="city", y="sales", title="Sales by city")
fig.show()
Interactive bar
import pandas as pd
import plotly.express as px
 
df = pd.DataFrame({
    "city": ["Pune", "Delhi", "Mumbai", "Bengaluru"],
    "sales": [120, 180, 90, 160],
})
 
fig = px.bar(df, x="city", y="sales", title="Sales by city")
fig.show()

Sorted bars

Sorted bar
import pandas as pd
import plotly.express as px
 
df = pd.DataFrame({
    "city": ["Pune", "Delhi", "Mumbai", "Bengaluru"],
    "sales": [120, 180, 90, 160],
}).sort_values("sales", ascending=False)
 
fig = px.bar(df, x="city", y="sales", title="Sales by city (sorted)")
fig.show()
Sorted bar
import pandas as pd
import plotly.express as px
 
df = pd.DataFrame({
    "city": ["Pune", "Delhi", "Mumbai", "Bengaluru"],
    "sales": [120, 180, 90, 160],
}).sort_values("sales", ascending=False)
 
fig = px.bar(df, x="city", y="sales", title="Sales by city (sorted)")
fig.show()

Sorting by value (instead of leaving categories alphabetical) turns the chart into a ranking at a glance — the reader doesn’t have to hunt for the tallest bar.

Horizontal bar

Horizontal
import plotly.express as px
 
fig = px.bar(df, x="sales", y="city", orientation="h", title="Sales by city")
fig.show()
Horizontal
import plotly.express as px
 
fig = px.bar(df, x="sales", y="city", orientation="h", title="Sales by city")
fig.show()

Horizontal bars are handy when category labels are long — they read left-to-right instead of getting squeezed or rotated on the x-axis.

sketch Bars respond to your mouse — hover one p5.js
Move your cursor over the canvas; the bar under it lifts in amber, mimicking a Plotly hover tooltip.

Tips

  • Sort by value when ranking matters more than alphabetical order.
  • Use color=color= to add a second grouping dimension (e.g. region) alongside category.
  • Keep the y-axis starting at zero — a truncated axis makes bar-height comparisons misleading.
diagram Bar chart hover interaction mermaid
What happens client-side when a reader moves the mouse over a bar.

Next

Continue to: Interactive Scatter Plots for exploring relationships between two variables.

🧪 Try It Yourself

Exercise 1 – Build a Bar Chart

Exercise 2 – Sort Before Plotting

Exercise 3 – Go Horizontal

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did