Extract plot task to a function