Skip to main content
 首页 » 编程设计

python之IPython自动开启matplotlib交互模式

2024年11月24日9think

我遇到了一些来自 IPython 的奇怪行为。我只需要干净地重新安装我的 miniconda,所以我现在有了新的 IPython 和 Matplotlib 版本。事实证明,IPython 会自动将 matplotlib 切换到交互模式(这有一个烦人的副作用,就是让我保存的图形在我的脚本中变成空白,因为它们是在我关闭窗口后保存的)。

这是一个例子:

Python 3.7.3 | packaged by conda-forge | (default, Jul  1 2019, 21:52:21)  
Type 'copyright', 'credits' or 'license' for more information 
IPython 7.6.1 -- An enhanced Interactive Python. Type '?' for help. 
 
In [1]: import matplotlib as mpl                                                                                                                               
 
In [2]: import matplotlib.pyplot as plt                                                                                                                        
 
In [3]: mpl.is_interactive()                                                                                                                                   
Out[3]: False 
 
In [4]: plt.plot([1,2])                                                                                                                                        
Out[4]: [<matplotlib.lines.Line2D at 0x7f0b0b048940>] 
 
In [5]: mpl.is_interactive()                                                                                                                                   
Out[5]: True 

有没有办法阻止这种行为?

请注意,我已经在常规 python 中测试了相同的代码,并且交互模式保持关闭。

请您参考如下方法:

这是 matplotlib 和 IPython 之间相互作用的一个错误,它在 matplotlib 版本 3.1.0 中引入(通过 #12637 )。它将在 matplotlib 3.2 中修复(通过 #14979 )。

您有以下选择: