Matplotlib Step-by-Step Learning Guide with python

amitmund June 03, 2026
pip install matplotlib

Verify installation:

import matplotlib
print(matplotlib.__version__)

Step 2: Import Matplotlib

import matplotlib.pyplot as plt

pyplot provides functions similar to drawing on a canvas.


Step 3: Your First Plot

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 15, 25]

plt.plot(x, y)
plt.show()

Step 4: Add Labels and Title

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 15, 25]

plt.plot(x, y)

plt.title("Sales Report")
plt.xlabel("Month")
plt.ylabel("Sales")

plt.show()

Step 5: Change Line Style

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 15, 25]

plt.plot(
    x,
    y,
    color="red",
    linestyle="--",
    marker="o",
    linewidth=2
)

plt.show()

Common Styles

  • - Solid line
  • -- Dashed line
  • : Dotted line
  • -. Dash-dot

Markers:

'o'  # circle
's'  # square
'^'  # triangle
'*'  # star

Step 6: Create a Bar Chart

import matplotlib.pyplot as plt

students = ["A", "B", "C", "D"]
marks = [80, 70, 90, 60]

plt.bar(students, marks)
plt.title("Student Marks")
plt.show()

Step 7: Create a Pie Chart

import matplotlib.pyplot as plt

sizes = [40, 30, 20, 10]
labels = ["Python", "Java", "C++", "JavaScript"]

plt.pie(sizes, labels=labels, autopct="%1.1f%%")
plt.show()

Step 8: Create a Scatter Plot

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 8, 5]

plt.scatter(x, y)
plt.show()

Step 9: Create a Histogram

import matplotlib.pyplot as plt

ages = [18,20,21,21,22,23,24,24,25,26,28,30]

plt.hist(ages)
plt.show()

Step 10: Multiple Lines

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]

y1 = [10, 20, 30, 40]
y2 = [5, 15, 25, 35]

plt.plot(x, y1, label="Product A")
plt.plot(x, y2, label="Product B")

plt.legend()
plt.show()

Step 11: Grid

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 15, 25]

plt.plot(x, y)
plt.grid(True)
plt.show()

Step 12: Save Chart as Image

import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [5, 10, 15]

plt.plot(x, y)
plt.savefig("chart.png")
plt.show()

Step 13: Subplots

import matplotlib.pyplot as plt

plt.subplot(1, 2, 1)
plt.plot([1, 2, 3], [4, 5, 6])
plt.title("Graph 1")

plt.subplot(1, 2, 2)
plt.plot([1, 2, 3], [6, 5, 4])
plt.title("Graph 2")

plt.show()

Step 14: Using NumPy with Matplotlib

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.show()

Step 15: Object-Oriented Style

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([1, 2, 3], [4, 5, 6])

ax.set_title("Sales")
ax.set_xlabel("Month")
ax.set_ylabel("Revenue")

plt.show()

Learning Roadmap

Week 1: Basics

  • Installation
  • Importing pyplot
  • Line plots
  • Titles
  • Labels
  • Legends
  • Grid

Practice: - Daily temperature chart - Student marks chart

Week 2: Chart Types

  • Bar charts
  • Pie charts
  • Scatter plots
  • Histograms

Practice: - Expense tracker - Population data - Survey results

Week 3: Customization

  • Colors
  • Line styles
  • Markers
  • Figure size
  • Multiple plots
plt.figure(figsize=(8, 5))

Week 4: Advanced Topics

  • NumPy integration
  • Subplots
  • Saving figures
  • Object-oriented API

Practice: - Weather dashboard - Stock price graph - Sales analytics report


Mini Project: Student Result Dashboard

import matplotlib.pyplot as plt

subjects = ["Math", "Science", "English", "History"]
marks = [85, 92, 78, 88]

plt.bar(subjects, marks)

plt.title("Student Report Card")
plt.xlabel("Subjects")
plt.ylabel("Marks")

plt.grid(True)

plt.show()
0 Likes
4 Views

Filters

No filters available for this view.

Reset All