Pie Charts and Histograms are commonly used visualizations in Data Analytics.
They help represent proportions and data distributions clearly.
First, import Matplotlib:
import matplotlib.pyplot as plt
1. Pie Chart
A Pie Chart shows the proportion or percentage of categories in a dataset.
Example: Market Share
companies = ["Company A", "Company B", "Company C", "Company D"]
market_share = [40, 25, 20, 15]plt.pie(market_share, labels=companies)
plt.title("Market Share Distribution")
plt.show()
Adding Percentage Labels
plt.pie(market_share, labels=companies, autopct="%1.1f%%")
plt.title("Market Share Distribution")
plt.show()
Exploding a Slice
explode = [0.1, 0, 0, 0]plt.pie(market_share, labels=companies, autopct="%1.1f%%", explode=explode)
plt.title("Market Share Distribution")
plt.show()
When to Use Pie Chart:
- Showing percentage distribution
- Comparing parts of a whole
- Presenting simple categorical breakdown
2. Histogram
A Histogram shows the distribution of numerical data.
It groups values into intervals (bins).
Example: Exam Scores Distribution
scores = [55, 60, 65, 70, 75, 80, 85, 90, 95, 60, 70, 80, 85]plt.hist(scores, bins=5)
plt.title("Exam Score Distribution")
plt.xlabel("Scores")
plt.ylabel("Frequency")
plt.show()
Adjusting Number of Bins
plt.hist(scores, bins=10)
plt.title("Exam Score Distribution")
plt.show()
When to Use Histogram:
- Understanding data distribution
- Detecting skewness
- Identifying outliers
- Analyzing frequency patterns
Pie Chart vs Histogram
Pie Chart:
- Used for categorical data
- Shows proportions
- Represents parts of a whole
Histogram:
- Used for numerical data
- Shows frequency distribution
- Represents data spread
Key Takeaway
Pie Charts help visualize proportions of categories, while Histograms help analyze the distribution of numerical data. Both are essential tools for understanding datasets in data analytics.