Every result this unit has produced so far has been read off as a printed number or a
True/False. For any simulation, estimator, or root-finding routine with behaviour that unfolds over something — time, iteration count, input size — a plot is often the fastest way to sanity-check that the behaviour looks right, and the fastest way to communicate a result to someone else. This note covers matplotlib.pyplot's basic plotting call, labelling a chart properly (axes, titles — not optional, per Remark 1 below), producing multiple figures, and controlling a plot's appearance with format strings and keyword arguments. docs/programme-profiles/library/introduction-to-python.md's Stage 8 names this the "hand-off to numerics and ML" — this note is the first, smallest step of that hand-off, deliberately staying at the "make one basic plot correctly" level and pointing forward, rather than sideways, to numpy arrays and pandas DataFrames.Learning objectives. By the end of this note you should be able to:
- produce a basic line plot from two parallel sequences (x-values and y-values) using
matplotlib.pyplot.plot, and explain what happens when only one sequence is supplied; - add a title and axis labels to a plot, and explain why an unlabelled plot is close to useless as a communication tool;
- manage multiple figures in a single script, and save a figure to a file rather than only displaying it on screen;
- control a plotted line's colour, marker, and line style using a format string, and adjust a plot's global appearance settings.
In this lesson
- The basic plotting call
- Multiple figures, and saving to a file
- Controlling appearance
Before you start
The full lesson, with worked examples and exercise solutions, is for members.
Read the full lessonNew to SelfMadeQuant.com? See membership options or browse the Python for Quantitative Finance syllabus.