简体   繁体   English

IPython自动开启matplotlib交互模式

[英]IPython automatically turning on matplotlib interactive mode

I'm experiencing some newly odd behavior from IPython.我遇到了一些来自 IPython 的新奇行为。 I just had to do a clean reinstall of my miniconda, so I now have fresh IPython and Matplotlib versions.我只需要重新安装 miniconda,所以我现在有了新的 IPython 和 Matplotlib 版本。 It turns out that IPython is automatically switching matplotlib to interactive mode (which has the annoying side effect of making my saved figures blank in my scripts, because they save after I close the window).事实证明,IPython 会自动将 matplotlib 切换到交互模式(这会产生令人讨厌的副作用,使我保存的图形在我的脚本中变为空白,因为它们会在我关闭窗口后保存)。

Here's an example:下面是一个例子:

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

Is there a way to stop this behavior?有没有办法阻止这种行为?

Note, I've tested the same code in regular python and interactive mode stays off.请注意,我已经在常规 python 中测试了相同的代码,并且交互模式保持关闭。

It's a bug in the interplay between matplotlib and IPython, which got introduced in matplotlib version 3.1.0 (via #12637 ).这是 matplotlib 和 IPython 之间相互作用的一个错误,它是在 matplotlib 3.1.0 版(通过#12637 )中引入的。 It will be fixed in matplotlib 3.2 (via #14979 ).它将在 matplotlib 3.2 中修复(通过#14979 )。

Options you have:您拥有的选项:

A workaround for this version of ipython/matplotlib would be to define a custom ioff():此版本的 ipython/matplotlib 的解决方法是定义自定义 ioff():

import matplotlib.pyplot as plt
import matplotlib as mpl

def my_ioff():
  f = plt.figure()
  plt.close(f)
  plt.ioff()

my_ioff()
print(mpl.is_interactive())
f = plt.figure()
print(mpl.is_interactive())
-----------
-> False
-> False

Whereas before we had:而在我们之前:

plt.ioff()
print(mpl.is_interactive())
f = plt.figure()
print(mpl.is_interactive())
-----------
-> False
-> True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM