bar() (Matplotlib)
ax.bar() draws a vertical bar chart, with one rectangular bar per category whose height encodes its value. ax.barh() draws the horizontal version. Bar charts compare values across discrete categories.
Matplotlib
Pass the category positions (or labels) and their heights: ax.bar(['A', 'B', 'C'], [3, 7, 5]). Use ax.barh() when category labels are long or there are many of them, since horizontal bars give the text more room.
To group or stack bars, offset their x positions or pass the bottom argument so one series sits on top of another. The width argument controls bar thickness, and color (or a list of colors) styles them.
bar returns a BarContainer holding the individual Rectangle patches, which you can loop over to annotate each bar with its value. Use a histogram, not a bar chart, when you are binning continuous data rather than comparing fixed categories.
Syntax
ax.bar(x, height, width=0.8) | ax.barh(y, width)Examples
Python
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
bars = ax.bar(["a", "b"], [3, 5])
print(type(bars).__name__)Output:
BarContainer