Data Visualization
A picture is worth a thousand rows. Master Matplotlib and Seaborn to tell stories with data.
A picture is worth a thousand rows. Master Matplotlib and Seaborn to tell stories with data. This hands-on tutorial focuses on practical implementation of data visualization concepts.
Data Visualization
Visualization is how we communicate insights. We'll use Matplotlib (the foundation) and Seaborn (the beautifier).
1. Matplotlib Basics π
Matplotlib is the most popular plotting library in Python.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 50]
# 1. Create a figure
plt.figure(figsize=(8, 5))
# 2. Plot data
plt.plot(x, y, color='blue', marker='o', linestyle='--')
# 3. Add labels
plt.title("Growth Over Time")
plt.xlabel("Days")
plt.ylabel("Users")
# 4. Show plot
plt.show()
2. Common Plot Types π
| Plot Type | Use Case | Function |
| :--- | :--- | :--- |
| Line Plot | Trends over time | plt.plot() |
| Bar Chart | Comparing categories | plt.bar() |
| Scatter Plot | Relationship between 2 variables | plt.scatter() |
| Histogram | Distribution of data | plt.hist() |
Scatter Plot Example
height = [160, 170, 180, 165, 175]
weight = [60, 70, 80, 65, 75]
plt.scatter(height, weight)
plt.title("Height vs Weight")
plt.xlabel("Height (cm)")
plt.ylabel("Weight (kg)")
plt.show()
3. Introduction to Seaborn π
Seaborn is built on top of Matplotlib. It makes complex plots easier and prettier.
import seaborn as sns
import pandas as pd
# Load built-in dataset
tips = sns.load_dataset("tips")
# Create a boxplot
sns.boxplot(x="day", y="total_bill", data=tips)
plt.title("Bill Distribution by Day")
plt.show()
Heatmaps
Great for showing correlation matrices.
corr = tips.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.show()
Interactive Challenge: The Artist
Create a visualization for this sales data.
Quiz
Quiz
Question 1 of 3Which plot is best for showing the distribution of a single variable?
Key Takeaways
β
Matplotlib gives you full control over every pixel.
β
Seaborn makes statistical plots (like heatmaps) easy.
β
Choose the right plot: Line for time, Bar for categories, Scatter for relationships.
What's Next?
We have the tools. Now let's put it all together to analyze a dataset from start to finish.
Next Chapter: Exploratory Data Analysis (EDA).