This issue doesn’t seem to be PyTorch-related, so you might get a better and faster answer in their support board or e.g. StackOverflow.
With that being said, you could try to switch to the Agg backend in matplotlib at the beginning of the script so avoid such issues.