Would it be better to have this logic shuffle/flip the data and only have the plotting once rather than creating inner functions which do both the data munging and the plotting (sorry if it become obvious later why you did it this way).