0
0
MatplotlibComparisonBeginner · 3 min read

Histogram vs Bar Chart in Matplotlib: Key Differences and Usage

A histogram in matplotlib shows the distribution of numerical data by grouping values into bins, while a bar chart displays categorical data with rectangular bars representing values. Histograms use continuous data and bins, whereas bar charts use discrete categories with separate bars.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of histograms and bar charts in matplotlib.

FeatureHistogramBar Chart
Data TypeContinuous numerical dataCategorical or discrete data
PurposeShow data distributionCompare values across categories
X-axisBins representing rangesCategories or labels
Bar WidthBars touch each otherBars separated by gaps
Function in matplotlibplt.hist()plt.bar()
Example UseAge distributionSales by product type
⚖️

Key Differences

A histogram groups continuous numerical data into intervals called bins and shows how many data points fall into each bin. This helps visualize the shape and spread of the data, such as whether it is skewed or uniform. The bars in a histogram touch each other to indicate the continuous nature of the data.

In contrast, a bar chart displays data for distinct categories or groups. Each bar represents a category's value, and bars are separated by spaces to emphasize that categories are separate and not continuous. Bar charts are useful for comparing quantities across different groups.

In matplotlib, you create histograms using plt.hist() which automatically calculates bins and frequencies, while bar charts use plt.bar() where you specify categories and their values directly.

⚖️

Code Comparison

This example shows how to create a histogram in matplotlib to display the distribution of random ages.

python
import matplotlib.pyplot as plt
import numpy as np

ages = np.random.randint(18, 70, size=100)
plt.hist(ages, bins=10, color='skyblue', edgecolor='black')
plt.title('Histogram of Ages')
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.show()
Output
A histogram plot with 10 bars touching each other showing frequency of ages in ranges from 18 to 69.
↔️

Bar Chart Equivalent

This example shows how to create a bar chart in matplotlib to compare sales across product categories.

python
import matplotlib.pyplot as plt

categories = ['Apples', 'Bananas', 'Cherries', 'Dates']
sales = [25, 40, 30, 10]

plt.bar(categories, sales, color='lightgreen', edgecolor='black')
plt.title('Sales by Product')
plt.xlabel('Product')
plt.ylabel('Sales')
plt.show()
Output
A bar chart with four separate bars spaced apart, each labeled by product and showing sales values.
🎯

When to Use Which

Choose a histogram when you want to understand the distribution of continuous numerical data, such as test scores or ages. It helps reveal patterns like skewness or modality.

Choose a bar chart when you want to compare values across distinct categories, like sales by region or counts of different items. Bar charts clearly show differences between groups.

Key Takeaways

Histograms visualize the distribution of continuous numerical data using bins.
Bar charts compare values across discrete categories with separate bars.
Use plt.hist() for histograms and plt.bar() for bar charts in matplotlib.
Histogram bars touch each other; bar chart bars have gaps to show category separation.
Choose histograms for data distribution insights and bar charts for category comparisons.