Page3/13
Bar Charts, Histograms & Scatter Plots · Page 1 of 2
Bar Charts for Comparisons
Bar Charts, Histograms & Scatter Plots
Bar Charts — Comparing Categories
Bar charts excel at comparing discrete categories:
categories = ["A", "B", "C", "D"]
values = [23, 45, 12, 67]
ax.bar(categories, values, color="#8b5cf6", alpha=0.8)
# Horizontal bars (better for long labels)
ax.barh(categories, values)
Grouped Bar Charts
import numpy as np
x = np.arange(len(categories))
width = 0.35
ax.bar(x - width/2, group1, width, label="Group 1")
ax.bar(x + width/2, group2, width, label="Group 2")
ax.set_xticks(x)
ax.set_xticklabels(categories)
Annotations
Add value labels directly on bars for clarity:
for bar in bars:
height = bar.get_height()
ax.annotate(f'{height}',
xy=(bar.get_x() + bar.get_width()/2, height),
xytext=(0, 3), textcoords="offset points",
ha='center', va='bottom', color='white')
main.py
Loading...
OUTPUT
▶Click "Run Code" to execute…