Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The pyplot does not save the figure

I have the code that uses pyplot function but the plot that is saved shows an empty area.
Removing variables did not give result. What is wrong??

import numpy as np
import matplotlib.pyplot as plt
 
print('Квадратическая функция y=ax^2+bx+c.')
a = 1 
b = 1 
c = 1 
 
x = np.linspace(-100, 100, 1000)
y = a*x**2 + b*x + c  
 
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
plt.savefig('mygraph.png')
print('the graph was saved')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

plt.show() should come after plt.savefig()

Explanation: plt.show() clears the whole thing, so anything afterwards will happen on a new empty figure

so try:

plt.savefig('mygraph.png')
plt.show()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading