Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 42 ยท Basic Charts

Bar Charts in Plotly

Create bar charts in Plotly with px.bar and go.Bar: grouped, stacked, relative and percent bars, horizontal bars, text labels, sorting, bar colors, gaps and patterns.

Bar charts compare values across categories. They are the right tool for "how much of each?" questions: sales per region, votes per candidate, errors per day. A bar's length is easy to judge, which makes bar charts one of the most readable chart types.

In Plotly, px.bar and go.Bar give you vertical and horizontal bars, several ways to combine multiple series (grouped, stacked, percent) and complete control over colors, labels and spacing.

A basic bar chart

Python A basic bar chart with labels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

text_auto=True prints each bar's value on the bar. The default orientation is vertical (v). For long category names, switch to horizontal bars by swapping the roles: px.bar(sales, x="units", y="region", orientation="h").

Grouped, stacked, relative and percent

With more than one series, the layout property barmode decides how bars of the same category are combined:

barmode Result
group Side by side: best for comparing series within a category
stack On top of each other
relative Stacked, with negative values going below zero (Express default)
overlay Drawn over each other, use opacity

For percent shares, set the layout property barnorm="percent" (or "fraction"); Express has no barnorm argument, so use fig.update_layout(barnorm="percent"). The data stays the same; plotly.js rescales each stack to 100%.

Grouped, stacked and 100% bars

Grouped bars make it easy to compare series within a category; stacked bars show the total and how it is made up. Percent stacks show composition and hide the totals, so say so in your title.

In Express, the long-format column that splits the bars is color, and barmode is an argument (Express bars default to relative, which behaves like stack for positive values):

Python px.bar with color, barmode and sorting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Sorting bars

The order of bars matters. For nominal categories, sort by value so readers can rank them. Sort the data first, or tell the axis how to order itself:

Python
sales = sales.sort_values("units", ascending=False)
fig = px.bar(sales, x="region", y="units")

# or let plotly.js sort by totals
fig.update_xaxes(categoryorder="total descending")

For ordered categories (months, sizes, ages) keep the natural order with category_orders. Horizontal bar charts list the first category at the bottom; add fig.update_yaxes(autorange="reversed") to show the first category at the top.

Colors, gaps and patterns

  • One color for all bars: fig.update_traces(marker_color="#636efa").
  • A color per bar (graph objects): marker_color=["red", "grey", "grey", "grey"] highlights one bar.
  • Color by value: px.bar(..., color="units", color_continuous_scale="Blues").
  • Spacing: bargap=0.2 (between categories) and bargroupgap=0.05 (between bars in a group).
  • Patterns for accessibility and print: pattern_shape="sex" or marker_pattern_shape="/".
  • Outline: marker_line=dict(color="black", width=1).
Python Highlight one bar, add labels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The grey-plus-highlight approach is a standard technique for telling a story: all attention goes to the one bar that matters.

Bars that do not start at zero

Bars encode value by length, so the axis for the bar value should start at zero, otherwise the chart exaggerates differences. If you need a floating bar (a range from a start to an end), use the base property: go.Bar(x=[3], y=["task"], base=[1], orientation="h") draws a bar from 1 to 4. This is how simple Gantt-like charts are built.

Common mistakes

  • Using bars for continuous x values such as time with fine resolution: use a line or a histogram.
  • Leaving categories unsorted, so a reader cannot find the biggest one.
  • Using a truncated axis (not starting at zero) for bars.
  • Making a 3D or pie replacement for a bar chart: a plain bar chart almost always reads better.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which layout property decides whether bars of several series are side by side or stacked?

    barmode accepts group, stack, relative and overlay.
  2. 2. How do you draw horizontal bars with px.bar?

    Horizontal bars use the same trace with orientation="h", with values on x and categories on y.
  3. 3. What does barnorm="percent" do?

    It normalizes each stack to 100, showing composition instead of totals.
  4. 4. Which technique highlights a single bar with graph objects?

    marker.color accepts an array with one color per bar.
  5. 5. In a horizontal bar chart the first category appears at the bottom. How can you put it on top?

    Reversing the y axis flips the order of categories.

Summary

  • Bars compare values across categories; px.bar and go.Bar draw them vertically or horizontally.
  • barmode (group, stack, relative, overlay) and barnorm control how series combine.
  • Sort bars by value or use category_orders for natural order.
  • Style with marker_color, bargap, text labels and patterns; start value axes at zero.

Next: Histograms