Python Matplotlib图形在复活后不关闭

Python Matplotlib图形在复活后不关闭,python,matplotlib,figure,Python,Matplotlib,Figure,请尝试以下代码: import matplotlib.pyplot as plt fig = plt.figure() #creates a pyplot figure plt.close(fig) #closes the figure fig.show() #resurrects the figure, that is, displays it plt.close(fig) #should close the figure, but does nothing 我的问题是: 为什么最后一行代码

请尝试以下代码:

import matplotlib.pyplot as plt
fig = plt.figure() #creates a pyplot figure
plt.close(fig) #closes the figure
fig.show() #resurrects the figure, that is, displays it
plt.close(fig) #should close the figure, but does nothing
我的问题是:

  • 为什么最后一行代码不关闭图形

  • 在像上面那样恢复图形之后,如何(以编程方式)关闭图形


  • 我认为调用
    close
    实际上会破坏图形所使用的任何底层资源,并阻止所有操作工作。默认后端允许窗口在您的案例中关闭后显示。尝试
    fig,ax=plt.subplot();ax.图(…);图1显示();plt.关闭(图);ax.图(…);图1显示()。只有你策划的第一件事才会出现。在那之后,所有的方法都不会做任何事情,因为后端被破坏了。我曾经遇到过类似的问题,创建了多个未正确关闭或删除的绘图。我通过不给plt赋值来解决这个问题。plt=无或在您的情况下fig=无。也许不是最好的解决方案,但却阻止matplotlib在一个绘图中绘制多个绘图。@疯狂的物理学家,你说得对。我认为使用x按钮手动关闭图形与plt.close(图)相同。但这表现得不同。我将在1-2天内删除此问题,并发布另一个与x按钮相关的问题。我认为您不应该删除此问题。它实际上相当不错。我希望像@tcaswell这样的真正的matplotlib大师能给出一个恰当的解释。我很想知道细节。