Bar Charts and Histograms
Visualise categorical counts with ax.bar() and data distributions with ax.hist(), adjusting bin count and edge colour.
Bar Charts and Histograms
Bar charts display categorical data: one bar per category, with bar height proportional to a value. They answer 'how much does each category have?'. Histograms display the distribution of a continuous numeric variable: the data is binned into intervals and bar height shows the count or frequency in each bin. They answer 'how are these values distributed?'. Both use ax.bar() and ax.hist() respectively.
ax.bar() for Vertical Bar Charts
ax.bar(x, height) draws a vertical bar chart where x is a sequence of category positions (or labels) and height is the bar value. Key parameters: width (default 0.8, controls bar width relative to the gap), color, edgecolor, and align ('center' or 'edge'). Returns a list of Rectangle artist objects.
import matplotlib.pyplot as plt
import numpy as np
categories = ['East', 'West', 'North', 'South']
values = [420, 380, 310, 290]
fig, ax = plt.subplots(figsize=(7, 4))
ax.bar(categories, values, color='steelblue', edgecolor='white', width=0.6)
ax.set_title('Regional Revenue')
ax.set_xlabel('Region')
ax.set_ylabel('Revenue (USD)')
# plt.show()
plt.close()All lessons in this course
- Figure and Axes Architecture
- Line and Scatter Plots
- Bar Charts and Histograms
- Labels, Titles, and Saving Figures