Topic 11 of 12 | π Lecture 11 β ICT582Topic11.pdf
Data Visualisation with Matplotlib
Pyplot interface, line/scatter/histogram/bar/pie charts, subplots, and chart customisation.
π― Learning Objectives
- Import and use the Matplotlib
pyplotmodule - Create figures with multiple subplots using
plt.subplot() - Plot line graphs with
plt.plot()and control format/colour/style - Add title, axis labels, limits, and legend
- Create scatter plots, histograms, bar charts, and pie charts
- Display plots using
plt.show()
π 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]
| Colour | Code | Marker | Code | Line | Code |
|---|---|---|---|---|---|
| Green | g | Circle | o | Solid | - |
| Blue | b | Star | * | Dashed | -- |
| Red | r | Square | s | Dotted | : |
| Black | k | Diamond | D | None | (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
| Function | What 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
- Know the difference between figure (window) and axes (a single plot inside the window).
plt.subplot(nrows, ncols, index)β index is 1-based (starts from 1, not 0).- Know plot format strings:
'go-'= green circles solid,'b*--'= blue stars dashed. - Know all 5 functions:
plt.title(),plt.xlabel(),plt.ylabel(),plt.legend(),plt.show(). - Know which chart type to use: line (trends), scatter (relationships), histogram (distribution), bar (categories), pie (proportions).
β Common Mistakes
- Calling
plt.subplot()after plotting βsubplotmust be called first to select axes. - Forgetting
plt.show()β nothing displays without it. - Confusing
plt.title()(for current axes) withplt.suptitle()(for whole figure). - Using 0-based index for subplot β index is 1-based.
plt.subplot(2,2,0)is an error.
β‘ Quick Recap
import matplotlib.pyplot as pltβ standard import.- Figure = window; Axes = one chart inside the window.
plt.subplot(r, c, i)β select axes i (1-based) in an rΓc grid.plt.plot(X, Y, 'format')β colour, marker, linestyle.- Customise:
plt.title(),plt.xlabel(),plt.ylabel(),plt.xlim(),plt.ylim(),plt.legend(). plt.show()β display (blocks until window closed).