Matplotlib Introduction

Learn Python's most popular data visualization library

📊 What is Matplotlib?

Matplotlib is Python's most popular plotting library for creating static, animated, and interactive visualizations. It provides a MATLAB-like interface and is the foundation for many other plotting libraries in Python.


import matplotlib.pyplot as plt

# Simple plot example
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

plt.plot(x, y)
plt.title('My First Plot')
plt.show()
                                    
2003
First Release
100M+
Downloads
Open
Source

🎯 Why Use Matplotlib?

📈

Versatile Plotting

Create line plots, bar charts, histograms, scatter plots, and more

Line Charts Bar Charts Histograms
🎨

Customizable

Full control over colors, styles, labels, and layouts

Colors Styles Themes
🔧

Easy to Learn

Simple syntax similar to MATLAB

Beginner Friendly MATLAB-like
🌐

Wide Support

Works with NumPy, Pandas, and other libraries

NumPy Pandas SciPy

🔹 What Can You Create?

Matplotlib can create a wide variety of visualizations

# Different types of plots you can make
import matplotlib.pyplot as plt
import numpy as np

# Line plot
plt.figure(figsize=(10, 6))

# Subplot 1: Line plot
plt.subplot(2, 2, 1)
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, 'b-')
plt.title('Line Plot')

# Subplot 2: Bar chart
plt.subplot(2, 2, 2)
categories = ['A', 'B', 'C', 'D']
values = [23, 45, 56, 78]
plt.bar(categories, values)
plt.title('Bar Chart')

# Subplot 3: Scatter plot
plt.subplot(2, 2, 3)
x = np.random.randn(50)
y = np.random.randn(50)
plt.scatter(x, y)
plt.title('Scatter Plot')

# Subplot 4: Histogram
plt.subplot(2, 2, 4)
data = np.random.normal(0, 1, 1000)
plt.hist(data, bins=30)
plt.title('Histogram')

plt.tight_layout()
plt.show()

🔹 Common Use Cases

Where matplotlib shines in real-world applications

📊 Data Analysis

  • Exploring datasets with quick visualizations
  • Finding patterns and trends in data
  • Creating publication-ready figures

🔬 Scientific Research

  • Plotting experimental results
  • Creating graphs for research papers
  • Visualizing mathematical functions

💼 Business Intelligence

  • Sales and revenue charts
  • Performance dashboards
  • Market analysis visualizations

🔹 Your First Matplotlib Plot

Let's create a simple plot to get started

# Step 1: Import matplotlib
import matplotlib.pyplot as plt

# Step 2: Prepare your data
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
temperatures = [22, 25, 23, 26, 24]

# Step 3: Create the plot
plt.plot(days, temperatures)

# Step 4: Add labels and title
plt.title('Weekly Temperature')
plt.xlabel('Day of Week')
plt.ylabel('Temperature (°C)')

# Step 5: Display the plot
plt.show()

# That's it! You've created your first matplotlib plot!

🧠 Test Your Knowledge

What does plt.show() do?

Which library is matplotlib most similar to?

What is the standard way to import matplotlib.pyplot?