Fixed memory leak due to creating figures in matplotlib