Data Science Machine Learning Data Analysis
38.6K subscribers
3.59K photos
31 videos
39 files
1.27K links
ads: @HusseinSheikho

This channel is for Programmers, Coders, Software Engineers.

1- Data Science
2- Machine Learning
3- Data Visualization
4- Artificial Intelligence
5- Data Analysis
6- Statistics
7- Deep Learning
Download Telegram
Topic: Python Matplotlib – From Easy to Top: Part 4 of 6: Advanced Charts – Histograms, Pie, Box, Area, and Error Bars

---

### 1. Histogram: Visualizing Data Distribution

Histograms show frequency distribution of numerical data.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=30, color='skyblue', edgecolor='black')
plt.title("Normal Distribution Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.grid(True)
plt.show()


Customizations:

bins=30 – controls granularity
density=True – normalize the histogram
alpha=0.7 – transparency

---

### 2. Pie Chart: Showing Proportions

labels = ['Python', 'JavaScript', 'C++', 'Java']
sizes = [45, 30, 15, 10]
colors = ['gold', 'lightgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0) # explode the 1st slice

plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%',
startangle=140, explode=explode, shadow=True)
plt.title("Programming Language Popularity")
plt.axis('equal') # Equal aspect ratio ensures pie is circular
plt.show()


---

### 3. Box Plot: Summarizing Distribution Stats

Box plots show min, Q1, median, Q3, max, and outliers.

data = [np.random.normal(0, std, 100) for std in range(1, 4)]

plt.boxplot(data, patch_artist=True, labels=['std=1', 'std=2', 'std=3'])
plt.title("Box Plot Example")
plt.grid(True)
plt.show()


Tip: Use vert=False to make a horizontal boxplot.

---

### 4. Area Chart: Cumulative Trends

x = np.arange(1, 6)
y1 = np.array([1, 3, 4, 5, 7])
y2 = np.array([1, 2, 4, 6, 8])

plt.fill_between(x, y1, color="skyblue", alpha=0.5, label="Y1")
plt.fill_between(x, y2, color="orange", alpha=0.5, label="Y2")
plt.title("Area Chart")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.legend()
plt.show()


---

### 5. Error Bar Plot: Showing Uncertainty

x = np.arange(0.1, 4, 0.5)
y = np.exp(-x)
error = 0.1 + 0.2 * x

plt.errorbar(x, y, yerr=error, fmt='-o', color='teal', ecolor='red', capsize=5)
plt.title("Error Bar Plot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.grid(True)
plt.show()


---

### 6. Horizontal Bar Chart

langs = ['Python', 'Java', 'C++', 'JavaScript']
popularity = [50, 40, 30, 45]

plt.barh(langs, popularity, color='plum')
plt.title("Programming Language Popularity")
plt.xlabel("Popularity")
plt.show()


---

### 7. Stacked Bar Chart

labels = ['2019', '2020', '2021']
men = [20, 35, 30]
women = [25, 32, 34]

x = np.arange(len(labels))
width = 0.5

plt.bar(x, men, width, label='Men')
plt.bar(x, women, width, bottom=men, label='Women')

plt.ylabel('Scores')
plt.title('Scores by Year and Gender')
plt.xticks(x, labels)
plt.legend()
plt.show()


---

### 8. Summary

Histograms show frequency distribution
Pie charts are good for proportions
Box plots summarize spread and outliers
Area charts visualize trends over time
Error bars indicate uncertainty in measurements
Stacked and horizontal bars enhance categorical data clarity

---

### Exercise

• Create a pie chart showing budget allocation of 5 departments.
• Plot 3 histograms on the same figure with different distributions.
• Build a stacked bar chart for monthly expenses across 3 categories.
• Add error bars to a decaying function and annotate the max point.

---

#Python #Matplotlib #DataVisualization #AdvancedCharts #Histograms #PieCharts #BoxPlots

https://t.iss.one/DataScienceM