EDIT: This question is a duplicate. Original question's link is posted above.
I am using Python to plot a set of values which are showing good on the Python terminal (using Jupyter NoteBook) but when I save it, the saved file when opened shows an empty (completely white) photo. Here's my code:
import matplotlib.pyplot as plt
plt.plot([1,3,4])
plt.show()
plt.savefig('E:/1.png')
You should save the plot before closing it: indeed, plt.show()
dislays the plot, and blocks the execution until after you close it; hence when you attempt to save on the next instruction, the plot has been destroyed and can no longer be saved.
import matplotlib.pyplot as plt
plt.plot([1,3,4])
plt.savefig('E:/1.png') # <-- save first
plt.show() # <-- then display
When you execute the show, plt
clears the graph. Just invert the execution of show
and savefig
:
import matplotlib.pyplot as plt
plt.plot([1,3,4])
plt.savefig('E:/1.png')
plt.show()
I think the plt.show()
command is "using up" the plot object when you call it. Putting the plt.savefig()
command before it should allow it to work.
Also, if you're using Jupyter notebooks you don't even need to use plt.show()
, you just need %matplotlib
inline somewhere in your notebook to have plots automatically get displayed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With