DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

How to Plot a Horizontal Bar Chart in Python Matplotlib

Use Matplotlib’s barh() to plot horizontal bars, label categories, control ordering, and add error bars or value labels.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Matplotlib’s barh() function to draw horizontal bars: pass category names or y-positions as its first argument and bar lengths as its second. Call ax.invert_yaxis() if you want the first category in your data to appear at the top.

Make a basic horizontal bar chart

This example uses the object-oriented Matplotlib interface, which attaches the chart to an explicit Axes:

import matplotlib.pyplot as plt

categories = ["Apples", "Bananas", "Cherries"]
values = [12, 19, 7]

fig, ax = plt.subplots()
ax.barh(categories, values)
ax.set_xlabel("Quantity")
ax.set_title("Fruit quantities")
ax.invert_yaxis()  # first category at the top
plt.show()

barh(y, width) maps the y argument to vertical positions or category labels and width to horizontal bar lengths. With unique strings, Matplotlib uses the category names as labels automatically. See the Matplotlib barh API reference.

For a compact script, the pyplot wrapper plt.barh(categories, values) is also available. Using ax.barh() is convenient when a figure contains multiple axes or when you want to configure a particular chart; Matplotlib’s horizontal bar chart example uses that form.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Put the first category at the top

By default, categorical positions increase upward, so the first item may appear at the bottom. Reverse the vertical axis after plotting to put the first supplied category at the top:

ax.invert_yaxis()

This changes the display order without rearranging the category and value lists.

Use numeric positions when labels repeat

Category strings are concise when each label is unique. If the displayed labels need to repeat, passing duplicate strings directly can place multiple bars at the same y-coordinate, making them overlap. Give each bar a distinct numeric position and set tick labels separately instead:

positions = [0, 1, 2]
labels = ["Group A", "Group A", "Group B"]
values = [12, 8, 15]

fig, ax = plt.subplots()
ax.barh(positions, values)
ax.set_yticks(positions, labels=labels)
ax.invert_yaxis()

Numeric positions also give you direct control over tick placement. The API reference documents y as either positions or categorical strings, and notes the overlap behavior for duplicate categories.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Adjust bar placement, appearance, and uncertainty

barh() accepts optional arguments for bar geometry and styling. Its default bar height is 0.8, its default horizontal baseline is zero, and align can be set to "center" or "edge".

  • height changes bar thickness.
  • left sets each bar’s horizontal starting point, which is also useful for constructing stacked bars by giving segments appropriate offsets.
  • color and edgecolor set fill and border colors; color can be a single value or a sequence.
  • xerr adds horizontal error bars. It can be a scalar, one value per bar, or a two-row array for separate lower and upper errors.

For example, pass values for error lengths alongside the bar widths:

ax.barh(categories, values, xerr=[1, 2, 1], color="steelblue")

Consult the API reference for the complete argument behavior.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Add values on the bars

barh() returns a BarContainer. Use Matplotlib’s bar_label() to add labels to the bars:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
bars = ax.barh(categories, values)
ax.bar_label(bars)

Set axis labels and a title as appropriate to clarify what the categories and values represent.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.