1

I use plt.savefig(pngpath, dpi=300), where the pngpath is the path where I want it to save. The png file as such is being created but is blank when I try and open it. Interestingly, the plt.show() right before this plt.savefig() works perfectly fine.

EDIT 1: As ONDRO pointed out, I used plt.show() after plt.savefig() and the plot saved perfectly as intended. Also, I will make sure to add minimal reproduceable code next time. Thank you ONDRO and everyone else for helping me out!

  • Please provide a [mcve] with a reproducible amount of code so that we can better understand how to help – G. Anderson Mar 22 '21 at 21:38
  • If you are doing this in jupyter, make sure `savefig()` is in the same cell as your plotting code. If `savefig()` is in a separate cell, the output file will be blank. – tdy Mar 23 '21 at 00:19

2 Answers2

0

So you haven't posted your code so I can't debug it for you. But try running this code and adepting it to your own.

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.savefig('myfig')
Adilius
  • 308
  • 2
  • 10
0

Using pyplot interface, you have to call plt.savefig() before plt.show(). If you change the order, the figure is destroyed once the window is closed and a new empty one is created by plt.savefig().

Ondro
  • 997
  • 5
  • 8