ICT582 Python Revision
🎯 Learning Objectives
πŸ“š Key Concepts

What is Matplotlib?

A popular Python library for graphing and data visualisation. Originally designed to recreate MATLAB-like plotting for Python. Used with NumPy for data visualisation.

pip install matplotlib                    # install once
import matplotlib.pyplot as plt          # convention: alias 'plt'

Figures and Axes

In Matplotlib:

  • A Figure is the whole window/image.
  • An Axes (singular) is one plotting area inside a figure. A figure can contain multiple axes arranged in a grid.

Example: a 2Γ—3 figure has 6 axes (one chart per cell).

plt.subplot() β€” Select an Axes

plt.subplot(nrows, ncols, index)
# Divides figure into nrowsΓ—ncols grid
# index (1-based) selects the current axes

plt.subplot(2, 2, 1)   # 2Γ—2 grid, axes 1 (top-left)
plt.subplot(2, 2, 4)   # 2Γ—2 grid, axes 4 (bottom-right)

Pyplot is stateful β€” it remembers the current axes. All subsequent plotting goes to the current axes.

plt.plot() β€” Line Plot

X = [1, 2, 3, 4, 5]
Y = [2, 3, 1, 7, 5]

plt.plot(X, Y, 'go-')      # green, circles, solid line
plt.plot(X, Y, 'b*--')     # blue, stars, dashed line
plt.plot(X, Y, 'r-')       # red, solid line

Format string: [colour][marker][linestyle]

ColourCodeMarkerCodeLineCode
GreengCircleoSolid-
BluebStar*Dashed--
RedrSquaresDotted:
BlackkDiamondDNone(omit)

Axes Labels, Limits, and Legend

plt.subplot(2, 2, 1)
X = [1,2,3,4]; Y = [2,3,1,7]
plt.plot(X, Y, 'go-', label='Green dots')
plt.title("Axes 1")        # title for current axes
plt.xlabel('X')             # x-axis label
plt.ylabel('Y')             # y-axis label
plt.xlim(0, 5)              # x-axis range 0 to 5
plt.ylim(0, 8)              # y-axis range 0 to 8
plt.legend(loc='best')     # show legend

plt.suptitle("A Simple Figure")  # title for whole figure
plt.show()                  # display (blocks until closed)

plt.show() copies the image from memory buffer to a window. The function doesn't return until the window is closed.

Scatter Plot

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(10)   # seed for reproducible results
data = {
    'a': np.arange(50),
    'b': np.arange(50) + 10 * np.random.randn(50),
    'c': np.random.randint(0, 50, 50),   # colour
    'd': np.abs(np.random.randn(50)) * 100  # size
}
plt.scatter('a', 'b', c='c', s='d', data=data)
plt.title('A Scatter Plot')
plt.show()

np.random Module

FunctionWhat it does
np.random.seed(n)Set seed for reproducibility
np.random.randn(n)n random floats from normal dist (mean=0, std=1)
np.random.randint(lo, hi, n)n random integers from lo to hi-1
np.random.random(n)n random floats between 0 and 1

Histogram, Bar Chart, Pie Chart

# Histogram β€” distribution of data
data = np.random.randn(1000)
plt.hist(data, bins=30)
plt.title("Histogram")
plt.show()

# Bar chart β€” comparing categories
categories = ["A", "B", "C", "D"]
values = [23, 45, 12, 38]
plt.bar(categories, values)
plt.title("Bar Chart")
plt.show()

# Pie chart β€” proportions
sizes = [30, 25, 20, 15, 10]
labels = ["Python", "Java", "C++", "C#", "Other"]
plt.pie(sizes, labels=labels, autopct="%.1f%%")
plt.title("Language Popularity")
plt.show()

Full Example β€” Visualising ndarray Data

import numpy as np
import matplotlib.pyplot as plt

def f(t):
    s = np.cos(2 * np.pi * t)
    e = np.exp(-t)
    return s * e             # ufuncs + element-wise ops

t1 = np.arange(0.0, 6.0, 0.1)   # coarse (70 points)
t2 = np.arange(0.0, 6.0, 0.02)  # fine (350 points)

plt.plot(t1, f(t1), 'go')   # green dots
plt.plot(t2, f(t2), 'r-')   # red line
plt.title('Damped oscillation', fontsize=18)
plt.show()
⚠️ Exam Focus
  1. Know the difference between figure (window) and axes (a single plot inside the window).
  2. plt.subplot(nrows, ncols, index) β€” index is 1-based (starts from 1, not 0).
  3. Know plot format strings: 'go-' = green circles solid, 'b*--' = blue stars dashed.
  4. Know all 5 functions: plt.title(), plt.xlabel(), plt.ylabel(), plt.legend(), plt.show().
  5. Know which chart type to use: line (trends), scatter (relationships), histogram (distribution), bar (categories), pie (proportions).
❌ Common Mistakes
⚑ Quick Recap
← Topic 10: NumPy Topic 12: Exam Guide β†’