only close all if there are any to close

avoids unnecessary gc on matplotlib < 1.4
Merge pull request #6612 from minrk/close-all-only-if-any

only close all if there are any to close