0

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')
Ivan
  • 17
  • 5

2 Answers2

0

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

The putting plt.savefig('mygraph.png') before plt.show() works.

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.savefig('mygraph.png')
plt.show()
print('the graph was saved')
Ivan
  • 17
  • 5