Lesson 4 of 4

Data Visualization with Matplotlib

Create compelling visualizations to communicate insights using Matplotlib for line plots, bar charts, scatter plots, and more.

32 minutes

Data Visualization with Matplotlib

Matplotlib is Python's foundational plotting library for creating static, animated, and interactive visualizations.

Why Visualize Data?

  • Discover patterns: See trends and relationships
  • Communicate insights: Share findings effectively
  • Identify outliers: Spot unusual data points
  • Compare data: Visualize differences and similarities

Basic Plot Types

Matplotlib supports many chart types:

  • Line plots: Show trends over time
  • Bar charts: Compare categories
  • Scatter plots: Show relationships between variables
  • Histograms: Display distributions
  • Pie charts: Show proportions
  • Box plots: Visualize statistical distributions

Creating Plots

Basic plotting workflow:

import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6))
plt.plot(x, y)
plt.title('My Plot')
plt.xlabel('X Label')
plt.ylabel('Y Label')
plt.show()

Customization

Make your plots professional:

  • Colors: Use color palettes and custom colors
  • Markers: Add shapes to data points
  • Line styles: Solid, dashed, dotted
  • Grid: Add gridlines for readability
  • Legends: Label multiple series
  • Annotations: Highlight important points

Multiple Plots

Create subplots for comparison:

fig, axes = plt.subplots(2, 2, figsize=(12, 10))
axes[0, 0].plot(x1, y1)
axes[0, 1].bar(categories, values)

Saving Figures

Export plots for reports:

plt.savefig('plot.png', dpi=300, bbox_inches='tight')

Best Practices

  • Choose the right chart type for your data
  • Use clear, descriptive labels
  • Keep designs simple and uncluttered
  • Use color purposefully
  • Consider your audience
  • Make plots accessible (color blind friendly)

Code Example

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# Sample data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [120, 135, 128, 145, 160, 155]
expenses = [80, 85, 82, 95, 105, 100]

# Create figure with subplots
fig, axes = plt.subplots(2, 2, figsize=(12, 10))

# 1. Line plot
axes[0, 0].plot(months, sales, marker='o', label='Sales', color='#2ecc71', linewidth=2)
axes[0, 0].plot(months, expenses, marker='s', label='Expenses', color='#e74c3c', linewidth=2)
axes[0, 0].set_title('Sales vs Expenses Over Time', fontsize=14, fontweight='bold')
axes[0, 0].set_xlabel('Month')
axes[0, 0].set_ylabel('Amount ($1000s)')
axes[0, 0].legend()
axes[0, 0].grid(True, alpha=0.3)

# 2. Bar chart
profit = [s - e for s, e in zip(sales, expenses)]
colors = ['#2ecc71' if p > 0 else '#e74c3c' for p in profit]
axes[0, 1].bar(months, profit, color=colors)
axes[0, 1].set_title('Monthly Profit', fontsize=14, fontweight='bold')
axes[0, 1].set_xlabel('Month')
axes[0, 1].set_ylabel('Profit ($1000s)')
axes[0, 1].axhline(y=0, color='black', linestyle='-', linewidth=0.5)
axes[0, 1].grid(True, alpha=0.3, axis='y')

# 3. Scatter plot
temperature = [65, 68, 72, 75, 80, 85]
ice_cream_sales = [200, 250, 300, 350, 450, 500]
axes[1, 0].scatter(temperature, ice_cream_sales, s=100, alpha=0.6, color='#3498db')
axes[1, 0].set_title('Ice Cream Sales vs Temperature', fontsize=14, fontweight='bold')
axes[1, 0].set_xlabel('Temperature (°F)')
axes[1, 0].set_ylabel('Sales')
axes[1, 0].grid(True, alpha=0.3)

# Add trend line
z = np.polyfit(temperature, ice_cream_sales, 1)
p = np.poly1d(z)
axes[1, 0].plot(temperature, p(temperature), "r--", alpha=0.8, label='Trend')
axes[1, 0].legend()

# 4. Pie chart
categories = ['Electronics', 'Clothing', 'Food', 'Books']
values = [35, 25, 20, 20]
colors_pie = ['#3498db', '#e74c3c', '#2ecc71', '#f39c12']
axes[1, 1].pie(values, labels=categories, autopct='%1.1f%%', colors=colors_pie, startangle=90)
axes[1, 1].set_title('Sales by Category', fontsize=14, fontweight='bold')

plt.tight_layout()
plt.savefig('data_visualizations.png', dpi=300, bbox_inches='tight')
plt.show()