13

In Matlab, at the beginning of every file, I usually write

clear; close all; clc

Is there something similar to this in Python? What do most people do when testing their scripts?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
TheRealFakeNews
  • 7,512
  • 16
  • 73
  • 114
  • 1
    In ipython %reset clears all variables –  Aug 25 '15 at 23:40
  • Ben's answer below should help with the figures. For the variables, try here: http://stackoverflow.com/questions/3543833/how-do-i-clear-all-variables-in-the-middle-of-a-python-script – rayryeng Aug 25 '15 at 23:52

5 Answers5

4

The catch here is that plt.show() is blocking and will not return to the script until the window is closed manually. You can try plt.draw(), which is interactive and will allow the script to continue running after the figure has been drawn.

Matplotlib Show Documentation

There is another question which discusses the difference between show and draw:

Difference between plt.show() and plt.draw()

Then the close should work.

Community
  • 1
  • 1
Ben
  • 380
  • 1
  • 9
1
  • Use %reset -f for clearing all the variables (without -f you have to confirm the clear command).

  • The equivalent command to MATLAB's clc is %clear in Spyder (for which you can use the shortcut "ctrl + L" as well).

  • Finally, plt.close('all') works like close all in MATLAB (you have to first import pyplot using the command import matplotlib.pyplot as plt).

0

I using either

print ("\n"*80)

Or

import os
clear = lambda: os.system('cls')  # On Windows System
clear()
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
0

To close the figure, use:

plt.close('all');
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0
def clear():
    print("\n"*80)
clear()
S.B
  • 13,077
  • 10
  • 22
  • 49
  • Now just use clear() function as when required in program clear console. – Priteesh Jul 22 '23 at 11:08
  • 3
    Answer needs supporting information Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – moken Jul 23 '23 at 09:48