π¨ Data Visualization: Painting Pictures with Data
Imagine you have a box full of colorful LEGO bricks. Each brick represents a piece of information. Now, instead of just counting them, what if you could arrange them to create beautiful pictures that tell a story? Thatβs exactly what data visualization does!
π The Magic of Seeing Data
Think about it like this: You have a treasure chest full of numbers. If someone just reads you a list of 100 numbers, youβll probably fall asleep! But what if those numbers became colorful bars, dots, or lines on a picture? Suddenly, the story jumps out at you!
Data visualization is like turning boring numbers into exciting pictures. And in Python, we have two amazing painting tools:
- Matplotlib - The grandfather of Python charts. Simple but powerful!
- Seaborn - The stylish grandchild. Makes everything look beautiful automatically!
ποΈ Matplotlib: Your First Paintbrush
What is Matplotlib?
Imagine you have a blank canvas and a box of crayons. Matplotlib gives you that canvas and all the tools to draw whatever you want. Itβs like the basic LEGO set - you can build anything!
Getting Started
import matplotlib.pyplot as plt
# Your first painting!
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.show()
Thatβs it! You just drew your first line! The first list [1, 2, 3, 4] tells Python where to put dots horizontally. The second list [1, 4, 9, 16] tells Python how high each dot should be.
π Line Charts: Connecting the Dots
Line charts are perfect for showing how things change over time. Like tracking how tall you grow each year!
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar', 'Apr']
ice_cream_sales = [100, 120, 180, 220]
plt.plot(months, ice_cream_sales)
plt.title('Ice Cream Sales')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.show()
Whatβs happening?
plt.plot()draws the lineplt.title()gives your picture a nameplt.xlabel()labels the bottomplt.ylabel()labels the sideplt.show()displays your masterpiece!
π Bar Charts: Building Blocks
Bar charts are like building towers with blocks. The taller the tower, the bigger the number!
import matplotlib.pyplot as plt
fruits = ['Apples', 'Bananas', 'Oranges']
quantities = [25, 40, 30]
plt.bar(fruits, quantities, color='orange')
plt.title('Fruit Basket')
plt.show()
Pro tip: Use color='orange' to pick your favorite color!
π΅ Scatter Plots: Sprinkle Dots Everywhere
What if your data points donβt connect in a line? Scatter plots just sprinkle dots wherever the data lives!
import matplotlib.pyplot as plt
height = [150, 160, 165, 170, 175]
weight = [50, 55, 60, 65, 70]
plt.scatter(height, weight, color='blue')
plt.title('Height vs Weight')
plt.xlabel('Height (cm)')
plt.ylabel('Weight (kg)')
plt.show()
Scatter plots help you see if two things are related. Do taller people tend to weigh more? The dots tell the story!
π₯§ Pie Charts: Slicing the Pizza
Pie charts show how a whole thing is divided into parts. Like cutting a pizza!
import matplotlib.pyplot as plt
activities = ['Sleep', 'School', 'Play', 'Eat']
hours = [8, 7, 5, 4]
plt.pie(hours, labels=activities, autopct='%1.0f%%')
plt.title('My Day')
plt.show()
Magic ingredient: autopct='%1.0f%%' shows the percentage on each slice!
π Histograms: Counting Groups
Histograms group your data into buckets. Imagine sorting marbles by size!
import matplotlib.pyplot as plt
test_scores = [65, 70, 75, 80, 85, 90, 72, 78, 82]
plt.hist(test_scores, bins=5, color='green')
plt.title('Test Score Distribution')
plt.xlabel('Score')
plt.ylabel('Students')
plt.show()
bins=5 means βdivide the data into 5 groupsβ
π¨ Customizing Your Art
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.figure(figsize=(8, 4))
plt.plot(x, y,
color='red',
linewidth=2,
linestyle='--',
marker='o')
plt.grid(True)
plt.show()
Your toolbox:
figsize=(8, 4)- Size of your canvascolor='red'- Line colorlinewidth=2- How thick the line islinestyle='--'- Dashed linemarker='o'- Circles at each pointgrid=True- Add helper lines
π Multiple Plots: The Gallery Wall
Sometimes you want to show multiple pictures side by side!
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
ax1.bar(['A', 'B', 'C'], [10, 20, 15])
ax1.set_title('Sales')
ax2.plot([1, 2, 3], [1, 4, 9])
ax2.set_title('Growth')
plt.tight_layout()
plt.show()
plt.subplots(1, 2) creates 1 row with 2 columns of pictures!
π Seaborn: The Style Master
What is Seaborn?
If Matplotlib is basic LEGO, Seaborn is like having a professional LEGO designer help you! It makes everything look beautiful with almost no effort.
import seaborn as sns
import matplotlib.pyplot as plt
# Seaborn makes things pretty automatically!
sns.set_theme() # Turn on the magic
π Seaborn Line Plot
import seaborn as sns
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar', 'Apr']
sales = [100, 150, 200, 250]
sns.lineplot(x=months, y=sales)
plt.title('Monthly Sales')
plt.show()
Notice how it looks more polished than basic Matplotlib!
π Seaborn Bar Plot
import seaborn as sns
import matplotlib.pyplot as plt
fruits = ['Apple', 'Banana', 'Orange']
counts = [30, 45, 25]
sns.barplot(x=fruits, y=counts, palette='pastel')
plt.title('Fruit Count')
plt.show()
palette='pastel' gives you beautiful coordinated colors!
π₯ Heatmaps: The Temperature Map
Heatmaps use colors to show intensity. Like a weather map showing hot and cold areas!
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
data = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
sns.heatmap(data, annot=True, cmap='YlOrRd')
plt.title('My Heatmap')
plt.show()
annot=True- Show numbers in each cellcmap='YlOrRd'- Yellow to Orange to Red colors
π¦ Box Plots: The Data Summary Box
Box plots show you a quick summary: the middle value, the spread, and any unusual values!
import seaborn as sns
import matplotlib.pyplot as plt
scores = [85, 90, 78, 92, 88, 76, 95, 89]
sns.boxplot(y=scores)
plt.title('Test Scores Summary')
plt.show()
The box shows where most of your data lives. The line in the middle is the median (the middle value).
π» Violin Plots: The Fancy Box Plot
Violin plots are like box plots but show the full shape of your data!
import seaborn as sns
import matplotlib.pyplot as plt
data = [65, 70, 70, 75, 75, 75, 80, 80, 85]
sns.violinplot(y=data, color='lightblue')
plt.title('Score Distribution Shape')
plt.show()
The wider parts show where most of your data is clustered!
π΅ Seaborn Scatter Plot
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')
sns.scatterplot(data=tips, x='total_bill',
y='tip', hue='day')
plt.title('Tips by Bill Amount')
plt.show()
hue='day' colors the dots by which day it was. Magic!
π Count Plot: Counting Categories
import seaborn as sns
import matplotlib.pyplot as plt
pets = ['Dog', 'Cat', 'Dog', 'Bird', 'Dog', 'Cat']
sns.countplot(x=pets, palette='Set2')
plt.title('Pet Count')
plt.show()
Seaborn counts for you! No need to calculate first.
π Distribution Plot: The Data Shape
import seaborn as sns
import matplotlib.pyplot as plt
ages = [22, 25, 27, 25, 30, 32, 25, 28, 26]
sns.histplot(ages, kde=True)
plt.title('Age Distribution')
plt.show()
kde=True adds a smooth curve showing the overall shape!
π Pair Plot: See Everything at Once
When you have multiple columns, pair plot shows ALL relationships!
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')
sns.pairplot(iris, hue='species')
plt.show()
This creates a grid showing how every column relates to every other column. Super powerful!
π¨ Seaborn Themes
import seaborn as sns
# Different looks!
sns.set_theme(style='darkgrid') # Dark background
sns.set_theme(style='whitegrid') # Light background
sns.set_theme(style='ticks') # Simple with ticks
π Quick Comparison
graph LR A[Data Visualization] --> B[Matplotlib] A --> C[Seaborn] B --> D[Full Control] B --> E[Basic Styling] B --> F[More Code Needed] C --> G[Built on Matplotlib] C --> H[Beautiful Defaults] C --> I[Less Code Needed]
| Feature | Matplotlib | Seaborn |
|---|---|---|
| Control | Maximum | Good |
| Beauty | Manual | Automatic |
| Code | More | Less |
| Learning | Start here | Add this |
π― When to Use What?
Use Matplotlib when:
- You need total control
- Youβre making custom visualizations
- You want simple, basic charts
Use Seaborn when:
- You want beautiful charts fast
- Youβre doing statistical analysis
- You have categorical data
π Your Visualization Journey
graph TD A[Start] --> B[Learn Matplotlib Basics] B --> C[Try All Chart Types] C --> D[Add Seaborn for Beauty] D --> E[Combine Both Powers] E --> F[Create Amazing Visuals!]
π‘ Remember These Magic Words
plt.plot()- Draw a lineplt.bar()- Draw barsplt.scatter()- Sprinkle dotsplt.show()- Display your art!sns.set_theme()- Make it prettysns.heatmap()- Color by valuesns.boxplot()- Show data summary
π The Big Picture
Data visualization is your superpower! Instead of staring at boring numbers, you can now:
- See patterns that hide in the data
- Tell stories that everyone understands
- Make decisions based on what you see
- Impress everyone with beautiful charts!
Remember: Matplotlib gives you the brushes, Seaborn makes you an artist. Together, they help you paint the story your data wants to tell!
Now go forth and visualize! Your data is waiting to become beautiful! π¨β¨