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
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
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
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.
Tips
- Sort by value when ranking matters more than alphabetical order.
- Use
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.
flowchart LR
A["Mouse moves over chart"] --> B{"Cursor over a bar's area?"}
B -- "Yes" --> C["Highlight that bar
show tooltip: category + value"]
B -- "No" --> D["No highlight"]
Check your understanding
Which call makes the bar chart of sales by city?
px.bar takes the category column as x and the value column as y.
Show answer
A — px.bar(df, x="city", y="sales") — px.bar takes the category column as x and the value column as y.
How does the lesson sort the bars into a ranking?
Sorting the DataFrame by value turns the chart into a ranking at a glance.
Show answer
A — sort_values("sales", ascending=False) before plotting — Sorting the DataFrame by value turns the chart into a ranking at a glance.
When are horizontal bars most useful?
Long labels read left to right instead of being squeezed or rotated. Use orientation="h" with the axes swapped.
Show answer
A — Long category labels — Long labels read left to right instead of being squeezed or rotated. Use orientation="h" with the axes swapped.
Why keep a bar chart y-axis starting at zero?
Bar length only reads honestly from a zero baseline.
Show answer
B — A truncated axis exaggerates differences in bar height — Bar length only reads honestly from a zero baseline.
Next
Continue to: Interactive Scatter Plots for exploring relationships between two variables.
🧪 Try It Yourself
Exercise 1 – Build a Bar Chart
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Sort Before Plotting
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Go Horizontal
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.