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 :

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()

Leave a Reply