University of Glasgow · FACULTY OF FINANCE

ACCFIN5277 Chap.12 Visualising Financial Data with Matplotlib

- one subject, every graph, every model, every mark
6 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 12 of 12 · ACCFIN5277

Visualising Financial Data with Matplotlib

Visualising Financial Data with Matplotlib closes the programming block by turning computed numbers into charts that communicate. The course session installs and imports the needed libraries, uses a function to download share prices, and builds line plots before moving to bar charts, histograms and time-series views with moving-average overlays.

This chapter follows the same path with data anyone can reproduce: the S&P 500 level for every 2025 trading day in the course market index file. The case study plots the daily close against its trailing 20-day average and reads the year from the picture: an early April low, a steady recovery and a year-end close well above the first trading day.

The worked method computes the overlay with NumPy before plotting, so its first and last values can be checked as numbers, then adds a title, axis labels and a legend so the chart stands on its own. A final section matches plot types to questions and explains why a moving average lags turning points. Throughout, data files stay unchanged and every chart is regenerated from code.

In this chapter

What this chapter covers

  • 01

    Importing libraries and loading price data

  • 02

    Line plots of a price or index series

  • 03

    Titles, axis labels and legends

  • 04

    Bar charts for categories and histograms for distributions

  • 05

    Trailing moving-average overlays and their lag

  • 06

    Reproducible charts generated from code

Worked example · free

Compute a 4-day trailing average before plotting it

Q [4 marks]. Six consecutive index closes are 5,900, 5,960, 6,020, 5,980, 6,100 and 6,160. Compute every 4-day trailing moving average, state how many there are, and say at which positions they should be plotted. The marks shown are an original practice weighting, not a university allocation.
The solid line plots the S&P 500 close on the 2025 trading days in the course index file and the dashed line its trailing 20-day average; the slump to 4,982.77 The solid line plots the S&P 500 close on the 2025 trading days in the course index file and the dashed line its trailing 20-day average; the slump to 4,982.77 in early April and the recovery to 6,845.50 at year end stand out clearly.5,0006,0007,000JanAprJulOct5,868.554,982.776,845.50daily close20-day average
The solid line plots the S&P 500 close on the 2025 trading days in the course index file and the dashed line its trailing 20-day average; the slump to 4,982.77 in early April and the recovery to 6,845.50 at year end stand out clearly.
  • 1Windows that fit: six closes minus four plus one gives three averages.
  • 2The averages are 5,965, 6,015 and 6,065, each the mean of four consecutive closes.
  • 1Plot them against the fourth, fifth and sixth days, the last day of each window, so the overlay lines up with the close.
There are three 4-day averages, 5,965, 6,015 and 6,065, plotted at days four to six because each belongs to the last day of its window.
Sia tip — Align a trailing average with the final day of its window; plotting it from day one shifts the whole overlay early.
Glossary

Key terms

Line plot
A chart joining successive observations in time order, used for price and index levels.
Moving average
The mean of the most recent fixed number of observations, recalculated as each new observation arrives.
Chart legend
The key that names each plotted series so a reader can tell overlapping lines apart.
Histogram
A bar chart of counts in consecutive value intervals that shows how a variable is distributed.
FAQ

Visualising Financial Data with Matplotlib FAQ

Do I need live Yahoo Finance data to practise plotting?

No. The session downloads share prices through a function built on the yfinance library, but any time series works for practice. The course market index file is a good choice because it never changes, so your chart and the numbers behind it can be compared with someone else's. Live downloads can differ from day to day as data providers revise histories.

Why does a moving average react late to a crash?

Each value averages a fixed window of past prices, so a sudden fall is diluted by the older, higher prices still inside the window. In 2025 the index bottomed on 8 April, but its 20-day average kept falling for about three more weeks. That delay is the cost of the smoothing that makes the overall trend easier to see.

Which chart should I use for monthly returns?

Monthly returns are separate categories rather than a continuous path, so a bar chart suits them: each month gets its own bar, and positive and negative months can be compared at a glance. A line plot would wrongly suggest that the value moves smoothly between months, while a histogram would discard the order of the months entirely.

What makes a chart acceptable in a written assessment?

It should stand alone: a title saying what is shown, labelled axes with units, a legend whenever more than one series appears, and a sensible scale. The reader should be able to state the main message without the surrounding text. Generating the chart from code also means it can be rebuilt exactly if a number in it is questioned.

How can I check the data before I plot it?

Print the size of each array and its first and last values, and confirm the dates are the ones you intended. The Data Wrangler extension for VS Code displays variables and a preview of their contents, which helps catch a wrong column or an empty selection before a misleading chart is drawn and pasted into your work.

Study strategy

Assessment move

Reproduce the 2025 index chart from the course data, first printing the number of closes and averages and their first and last values, then plotting. Practise one plot of each type, line, bar and histogram, on the same dataset and write one sentence on what each reveals.

Before saving any figure, check the title, both axis labels, the legend and the alignment of any overlay, and keep the plotting code in a function you can reuse. Compare your chart against the printed numbers to confirm nothing was shifted or dropped.

Working through Visualising Financial Data with Matplotlib in ACCFIN5277? Sia is AskSia’s AI Finance tutor — ask any ACCFIN5277 Visualising Financial Data with Matplotlib question and get a clear, step-by-step explanation grounded in how ACCFIN5277 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 26 of your subjects at University of Glasgow, plus 1,000+ more Bibles across the AskSia library.
Sia - your ACCFIN5277 tutor, unlimited, worked the way the exam marks it
The full 3-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works
ACCFIN5277 · Finance Programming in Python and Databases - independent study guide on the AskSia Library. More University of Glasgow subjects · Microeconomics across all universities