📊 Matplotlib Bar Charts
Last Updated: 06 Nov 2025
Bar chart is used to compare categories. You can use vertical bars (bar) or horizontal bars (barh).
Hinglish Tip 🗣: Bar chart ko “items ki comparison” dikhane ke liye use karte hain — jaise sales, marks, count etc.
Basic Vertical Bar Chart
import matplotlib.pyplot as plt
names = ["A", "B", "C"]
values = [10, 25, 15]
plt.bar(names, values)
plt.show()
Basic Horizontal Bar Chart
import matplotlib.pyplot as plt
names = ["A", "B", "C"]
values = [10, 25, 15]
plt.barh(names, values)
plt.show()
Useful Parameters for Bar Plot
color- Color of the barwidth- Width of the barheight- Height of the barbottom- Bottom position of the baralign- Alignment of the bar(e.g., 'center', 'edge')edgecolor- Color of the bar edgelinewidth- Width of the bar edgetick_label- Labels for the bar ticks
Example For Vertical Bar Chart
import matplotlib.pyplot as plt
names = ["A", "B", "C", "D"]
values = [12, 30, 22, 17]
plt.figure(figsize=(8, 4))
plt.bar(
names,
values,
color="skyblue",
width=0.6,
edgecolor="black",
linewidth=1.2,
alpha=0.9,
label="Sales Count"
)
plt.title("Vertical Bar Chart")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.legend()
plt.grid(axis="y", linestyle="--", alpha=0.5)
plt.show()
Example For Horizontal Bar Chart
plt.figure(figsize=(8, 4))
plt.barh(
names,
values,
color="orange",
height=0.6,
edgecolor="black",
linewidth=1,
alpha=0.9,
label="Sales Count"
)
plt.title("Horizontal Bar Chart")
plt.xlabel("Values")
plt.ylabel("Categories")
plt.legend()
plt.grid(axis="x", linestyle="--", alpha=0.5)
plt.show()
Grouped (Side-by-Side) Bar Chart
Useful when you want to compare multiple categories.
import numpy as np
names = ["A", "B", "C", "D"]
x = np.arange(len(names))
values1 = [12, 30, 22, 17]
values2 = [10, 28, 18, 20]
plt.figure(figsize=(8, 4))
plt.bar(x - 0.2, values1, width=0.4, label="2024")
plt.bar(x + 0.2, values2, width=0.4, label="2025")
plt.xticks(x, names)
plt.legend()
plt.show()
Stacked Bar Chart
Useful when you want to compare multiple categories.
values1 = [12, 30, 22, 17]
values2 = [10, 20, 15, 5]
plt.bar(names, values1, label="Male")
plt.bar(names, values2, bottom=values1, label="Female")
plt.legend()
plt.show()