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.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minute#1 Best Overall
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.
Rank #2
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.
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".
heightchanges bar thickness.leftsets each bar’s horizontal starting point, which is also useful for constructing stacked bars by giving segments appropriate offsets.colorandedgecolorset fill and border colors; color can be a single value or a sequence.xerradds 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.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.
Best Value
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.
Quick Recap
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.




