在写 Matlab 代码时,一般都有一个习惯,即在代码最开始的地方清空所有变量,如下所示:

clear ;
close all ;
clc ;

那么,在 Python 中应该如何执行类似上述的操作呢?应在代码最开始的地方添加如下内容:

%reset -f    # 相当于 Matlab 中的 clear, -f 代表 force,即强制执行

import matplotlib.pyplot as plt
plt.close("all")    # 相当于 Matlab 中的 close all, 即关闭所有图片

%clear    # 相当于 Matlab 中的 clc, 即清空命令窗口

参考资料