0
import time
import matplotlib.pyplot as plt

xvalues = [1,2,3,4,5,6,7,8,9]
yvalues = [1,3,5,9,8,7,8,5,6]
plt.xlabel('time in hours')
plt.ylabel('ph')
plt.plot([xvalues],[yvalues], 'ro')
plt.axis ([0,10,0,15])
plt.show()
time.sleep(1)
clf()

I want to make a figure with a plot, and then delete the figure after a specific time. But when I try it I get the error: undefined name on the last line where I want to delete the figure.

Arpit Solanki
  • 9,567
  • 3
  • 41
  • 57

1 Answers1

1

Concerning the error: clf() is not defined, you would want to use plt.clf() instead.

However, plt.clf() will not delete the figure. It only clears the figure. You may want to read When to use cla(), clf() or close() for clearing a plot in matplotlib?

Unfortunately it is not entirely clear from the question what the expected behaviour of the code is. If running it as a script, the figure window will stay open until you manually close it; only then any code after plt.show() will be executed. The use of time.sleep() would then not make any sense and deleting the figure is unnecessary, since the script stops afterwards anyways, clearing the memory.

If instead you want to run this in interactive mode (plt.ion()) you can use plt.pause(1) to make a 1 second pause and then close the figure.

import matplotlib.pyplot as plt

plt.ion()

xvalues = [1,2,3,4,5,6,7,8,9]
yvalues = [1,3,5,9,8,7,8,5,6]
plt.xlabel('time in hours')
plt.ylabel('ph')
plt.plot([xvalues],[yvalues], 'ro')
plt.axis ([0,10,0,15])
plt.show()

plt.pause(1)
plt.close()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712