Matplotlib Get Started

Install and set up matplotlib for data visualization

🚀 Getting Started

Learn how to install matplotlib and create your first visualization. We'll cover installation, basic setup, and your first plot in just a few minutes.


# Install matplotlib using pip
pip install matplotlib

# Or using conda
conda install matplotlib
                                    
1
Command
Easy
Installation
Ready
To Plot

📦 Installation Methods

🐍

Using pip

Standard Python package installer

pip install matplotlib
🐍

Using conda

Anaconda package manager

conda install matplotlib
📦

With NumPy

Install both libraries together

pip install matplotlib numpy
🔬

Full Data Stack

Install with pandas and scipy

pip install matplotlib pandas numpy

🔹 Verify Installation

Check if matplotlib is installed correctly

# Test your matplotlib installation
import matplotlib
print(f"Matplotlib version: {matplotlib.__version__}")

# Test plotting capability
import matplotlib.pyplot as plt
print("Matplotlib is ready to use!")

# Quick test plot
plt.plot([1, 2, 3], [1, 4, 9])
plt.title('Installation Test')
plt.show()

🔹 Basic Import Patterns

Common ways to import matplotlib in your code

# Most common import (recommended)
import matplotlib.pyplot as plt

# Import specific functions
from matplotlib.pyplot import plot, show, title

# Import with numpy (common combination)
import matplotlib.pyplot as plt
import numpy as np

# For interactive notebooks
%matplotlib inline  # Jupyter notebook magic command

🔹 Your First Plot

Create a simple line plot step by step

# Step-by-step first plot
import matplotlib.pyplot as plt

# 1. Create some data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# 2. Create the plot
plt.plot(x, y)

# 3. Add a title
plt.title('My First Matplotlib Plot')

# 4. Add axis labels
plt.xlabel('X values')
plt.ylabel('Y values')

# 5. Show the plot
plt.show()

print("Congratulations! You created your first plot!")

🔹 Interactive vs Non-Interactive

Understanding different ways to display plots

🖥️ Interactive Mode (Default)

  • Plots appear in separate windows
  • Can zoom, pan, and interact with plots
  • Use plt.show() to display

📓 Inline Mode (Jupyter)

  • Plots appear directly in notebook cells
  • Use %matplotlib inline magic command
  • Static images, no interaction
# For Jupyter notebooks
%matplotlib inline
import matplotlib.pyplot as plt

# Create plot
plt.plot([1, 2, 3], [1, 4, 9])
plt.title('Inline Plot')
# No need for plt.show() in inline mode

# For interactive plots in Jupyter
%matplotlib widget
# Enables interactive plots in notebooks

🔹 Common Setup Issues

Solutions to typical installation problems

# Issue 1: Import error
try:
    import matplotlib.pyplot as plt
    print("✅ Matplotlib imported successfully")
except ImportError:
    print("❌ Matplotlib not found. Install with: pip install matplotlib")

# Issue 2: Display backend problems
import matplotlib
print(f"Current backend: {matplotlib.get_backend()}")

# Change backend if needed
matplotlib.use('TkAgg')  # For interactive plots
# matplotlib.use('Agg')  # For non-interactive (saving only)

# Issue 3: Font warnings (can be ignored)
import warnings
warnings.filterwarnings('ignore', category=UserWarning)

🧠 Test Your Knowledge

What command installs matplotlib using pip?

Which import statement is most commonly used?

What does %matplotlib inline do?