“Fail to create pixmap with Tk_GetPixmap in ImgPhotoInstanceSetSize”
It’s not immediately clear just why matplotlib generates this error. I’m guessing by the namespace that it uses Tk under the hood. Okay. And apparently Tk has issues on Windows XP and earlier (not sure about later versions of Windows) where it eventually runs afoul of either a memory leak or a bitmap handle reference limitation … or something. Information is kind of sketchy.
And what’s worse, the results of this bug are disastrous. It doesn’t stacktrace. It simply crashes Python with the message to stderr:
“This application has requested the Runtime to terminate it in an unusual way.
Please contact the application’s support team for more information.”
But fear not! There’s actually a simple solution. Don’t import matplotlib globally. Import it locally instead. This causes whatever limitation gets exceeded to be reset because the module gets unloaded once it goes out of reference. And if your code somehow generates so many charts in one function that even that isn’t enough to fix your matplotlib woes, then take it one step further and reload the library using the reload statement.
Yes, it really is that simple.
Though it’d be awfully nice if matplotlib (or Tk) were to just fix this problem in the first place. As of the time of writing this bug is still in matplotlib up to version 0.99.1.