Bar Charts

Matplotlib is a Python library for creating charts and visualizations — and the bar chart is its workhorse for comparing discrete categories like products, regions, or months at a glance.

Learn Bar Charts in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll build vertical and horizontal bars, color them, group them side by side, and label each bar with its value.

A bar chart compares values across categories. Pass two lists to plt.bar() : the category labels and their values. Matplotlib draws one rectangle per category, with height equal to the value.

What you'll see: four steel-blue bars side by side. Bananas is the tallest at 58, Dates is the shortest at 25, and the fruit names sit neatly along the x-axis with a title above the chart.

When category names are long, horizontal bars read better. Swap plt.bar() for plt.barh() . You can also pass a list of colors to highlight individual bars.

What you'll see: five horizontal bars stacked from bottom to top, each in its own color. Python's bar stretches furthest to the right at 88, and the long language names sit comfortably on the y-axis where they have room to breathe.

To compare two series across the same categories, draw grouped bars . Build a numeric position array with np.arange() , then shift one series left and the other right by half the bar width. A small loop adds the value above each bar.

What you'll see: four pairs of bars, one pair per quarter. The 2023 bar sits just left of center and 2024 just right, the legend distinguishes the two years by color, and a small number floats above every bar showing its exact value.

Replace each ___ to build a colored bar chart and label its axes.

Your category list and value list have different lengths. Every category needs exactly one value.

You forgot to offset the positions. Use x - width/2 and x + width/2 with a width below 0.5.

Plot how many cups of each drink were sold, then add a value above every bar.

Lesson 7 complete — you can compare categories with bars!

You drew vertical and horizontal bars, colored them individually, grouped two series side by side, and labeled each bar with its value.

🚀 Up next: Histograms — see the shape of a distribution by bucketing your data.

Practice quiz

Which function draws vertical bars?

  • plt.barh()
  • plt.bar()
  • plt.hist()
  • plt.scatter()

Answer: plt.bar(). plt.bar() draws vertical bars with the value as the height.

Which function draws horizontal bars?

  • plt.barh()
  • plt.bar()
  • plt.plot()
  • plt.pie()

Answer: plt.barh(). plt.barh() draws horizontal bars, handy for long category labels.

In a vertical bar chart, what does each bar's height represent?

  • The category name
  • The number of bars
  • The value for that category
  • The bar width

Answer: The value for that category. With plt.bar() the height equals the value for that category.

How do you give each bar its own color?

  • Use plt.barh() only
  • Set marker per bar
  • Call plt.color()
  • Pass a list of colors to color=

Answer: Pass a list of colors to color=. Pass color=['red','green','blue'] — one color per bar.

What is used to build numeric positions for grouped bars?

  • np.arange(len(categories))
  • plt.xticks()
  • np.meshgrid()
  • range strings

Answer: np.arange(len(categories)). np.arange gives integer positions you can offset for each group.

To place two grouped series side by side, you offset positions by:

  • the full bar width
  • half the bar width (x - width/2 and x + width/2)
  • one unit
  • the number of categories

Answer: half the bar width (x - width/2 and x + width/2). Shifting each series by width/2 puts the pair on either side of the center.

After offsetting grouped bars, how do you label categories under each pair?

  • plt.legend()
  • plt.title()
  • plt.xticks(x, labels)
  • plt.grid()

Answer: plt.xticks(x, labels). plt.xticks(x, labels) sets the category labels at the group positions.

Which method returns a single bar's height when adding value labels?

  • bar.height()
  • bar.value()
  • bar.top()
  • bar.get_height()

Answer: bar.get_height(). bar.get_height() returns the height so you can place text above it.

A bar chart is the right choice mainly for what kind of data?

  • Categorical (named groups)
  • Continuous time series
  • 3D surfaces
  • Geographic maps

Answer: Categorical (named groups). Bar charts compare values across discrete categories.

What causes a 'shape mismatch' error in plt.bar()?

  • Too many colors
  • The category and value lists have different lengths
  • Missing a title
  • Using barh instead of bar

Answer: The category and value lists have different lengths. Every category needs exactly one value; unequal lengths error out.