Histogram vs Bar Chart in Matplotlib: Key Differences and Usage
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.
| Feature | Histogram | Bar Chart |
|---|---|---|
| Data Type | Continuous numerical data | Categorical or discrete data |
| Purpose | Show data distribution | Compare values across categories |
| X-axis | Bins representing ranges | Categories or labels |
| Bar Width | Bars touch each other | Bars separated by gaps |
| Function in matplotlib | plt.hist() | plt.bar() |
| Example Use | Age distribution | Sales 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.
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()
Bar Chart Equivalent
This example shows how to create a bar chart in matplotlib to compare sales across product categories.
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()
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
plt.hist() for histograms and plt.bar() for bar charts in matplotlib.