Refactored plot generation code for improved flexibility.