fixed memory leak in matplotlib code