ACCFIN5277 Chap.12 Visualising Financial Data with Matplotlib
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.
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
- 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.
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.
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.
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.