何时使用 cla()、clf() 或 close() 清除 matplotlib 中的绘图? [英] When to use cla(), clf() or close() for clearing a plot in matplotlib?

查看:33
本文介绍了何时使用 cla()、clf() 或 close() 清除 matplotlib 中的绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matplotlib 提供以下功能:

Matplotlib offers these functions:

cla()   # Clear axis
clf()   # Clear figure
close() # Close a figure window

文档并没有提供很多关于这些功能之间的区别的见解.我应该在什么时候使用每个函数,它到底有什么作用?

The documentation doesn't offer a lot of insight into what the difference between these functions is. When should I use each function and what exactly does it do?

推荐答案

它们都做不同的事情,因为 matplotlib 使用分层顺序,其中图形窗口包含可能由许多轴组成的图形.此外,还有来自 pyplot 接口的函数和 Figure 类上的方法.我将在下面讨论这两种情况.

They all do different things, since matplotlib uses a hierarchical order in which a figure window contains a figure which may consist of many axes. Additionally, there are functions from the pyplot interface and there are methods on the Figure class. I will discuss both cases below.

pyplot 是一个模块,它收集了几个允许以函数方式使用 matplotlib 的函数.我在这里假设 pyplot 已作为 import matplotlib.pyplot as plt 导入.在这种情况下,有三个不同的命令可以删除内容:

pyplot is a module that collects a couple of functions that allow matplotlib to be used in a functional manner. I here assume that pyplot has been imported as import matplotlib.pyplot as plt. In this case, there are three different commands that remove stuff:

参见 matplotlib.pyplot 函数:

See matplotlib.pyplot Functions:

plt.cla() 清除一个轴,即当前图形中当前活动的轴.它使其他轴保持不变.

plt.cla() clears an axes, i.e. the currently active axes in the current figure. It leaves the other axes untouched.

plt.clf() 清除整个当前图形 及其所有轴,但保持窗口打开,以便它可以重复用于其他绘图.

plt.clf() clears the entire current figure with all its axes, but leaves the window opened, such that it may be reused for other plots.

plt.close() 关闭一个窗口,如果没有另外指定,它将是当前窗口.

plt.close() closes a window, which will be the current window, if not specified otherwise.

哪种功能最适合您取决于您​​的用例.

Which functions suits you best depends thus on your use-case.

close() 函数还允许您指定应该关闭哪个窗口.参数可以是使用 figure(number_or_name) 创建窗口时赋予窗口的数字或名称,也可以是获得的图形实例 fig,即使用<代码>无花果=图形().如果没有给 close() 提供参数,当前活动的窗口将被关闭.此外,还有语法 close('all'),用于关闭所有数字.

The close() function furthermore allows one to specify which window should be closed. The argument can either be a number or name given to a window when it was created using figure(number_or_name) or it can be a figure instance fig obtained, i.e., usingfig = figure(). If no argument is given to close(), the currently active window will be closed. Furthermore, there is the syntax close('all'), which closes all figures.

此外,Figure 类提供清除图形的方法.我将在下面假设 figFigure 的一个实例:

Additionally, the Figure class provides methods for clearing figures. I'll assume in the following that fig is an instance of a Figure:

fig.clf()清除整个图形.仅当 fig 是当前图形时,此调用才等效于 plt.clf().

fig.clf() clears the entire figure. This call is equivalent to plt.clf() only if fig is the current figure.

fig.clear()fig.clf()

请注意,即使 del fig 也不会关闭关联的图形窗口.据我所知,关闭图形窗口的唯一方法是使用 plt.close(fig) 如上所述.

Note that even del fig will not close the associated figure window. As far as I know the only way to close a figure window is using plt.close(fig) as described above.

这篇关于何时使用 cla()、clf() 或 close() 清除 matplotlib 中的绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆