I typically write 100–200 lines of code each time I develop a scientific figure that is destined for publication. This is a dangerous length because it’s easy to create a functioning mess. With shorter code fragments, it’s feasible to start over from scratch, and with thousands of lines of code, it makes sense to invest time upfront to organise and plan. But in between these extremes lurks the appeal to write a script that feels coherent at the time, but just creates problems for future you.
Let’s say you want to create a moderately complicated figure like this:

A script for this figure could be envisaged as a series of sequential steps:
- Read data in from a csv file
- Remove any flagged data
- Create four subplots
- Plot the first line of data against time
- Label the y axis
- Set the y axis limit
- Repeat steps 4–6 for the second and third lines of data
- Add the coloured contours and grey contour lines
- Label the time axis
- Add various annotations