refactor plotting code to use shared functions