Try using matplotlib cleanup decorator