Data Visualization with Matplotlib & Seaborn
Create professional charts and plots to communicate data insights effectively.
Matplotlib: The Foundation
Matplotlib is the most widely used library for 2D graphics in Python.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title("Simple Line Plot")
plt.xlabel("Time")
plt.ylabel("Value")
plt.show()
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title("Simple Line Plot")
plt.xlabel("Time")
plt.ylabel("Value")
plt.show()
Seaborn: Statistical Graphics
Seaborn is built on top of Matplotlib and provides a high-level interface for creating attractive statistical plots.
import seaborn as sns
# Load an example dataset
tips = sns.load_dataset("tips")
# Create a violin plot
sns.violinplot(x="day", y="total_bill", data=tips)
plt.show()
# Load an example dataset
tips = sns.load_dataset("tips")
# Create a violin plot
sns.violinplot(x="day", y="total_bill", data=tips)
plt.show()
Common Plot Types
- Line Plot: Shows trends over time.
- Scatter Plot: Shows relationships between two numerical variables.
- Histogram: Shows the distribution of a single numerical variable.
- Bar Chart: Compares values across categorical groups.
- Heatmap: Visualizes correlation matrices or 2D data grids.
โ Practice (20 minutes)
- Create a scatter plot comparing "Height" and "Weight" from a sample list.
- Create a bar chart showing the population of 5 different cities.
- Load the "iris" dataset from Seaborn and create a pair plot (
sns.pairplot). - Customize a plot with a different color palette and a legend.