TheRealFakeNews
TheRealFakeNews

Reputation: 8173

Python equivalent of Matlab's clear, close all, clc

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?

Upvotes: 13

Views: 45680

Answers (5)

Priteesh
Priteesh

Reputation: 1

def clear():
    print("\n"*80)
clear()

Upvotes: 0

M. Reza Andalibi
M. Reza Andalibi

Reputation: 155

  • 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).

Upvotes: 1

AlbiBone
AlbiBone

Reputation: 1

To close the figure, use:

plt.close('all');

Upvotes: 0

kashanipour
kashanipour

Reputation: 29

I using either

print ("\n"*80)

Or

import os
clear = lambda: os.system('cls')  # On Windows System
clear()

Upvotes: 0

Ben
Ben

Reputation: 390

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.

Upvotes: 4

Related Questions