如何在 Spyder/IPython/matplotlib 中再次获得交互式绘图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23585126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:12:24  来源:igfitidea点击:

How do I get interactive plots again in Spyder/IPython/matplotlib?

pythonmatplotlibipythonspyder

提问by endolith

I upgraded from Python(x,y) 2.7.2.3 to 2.7.6.0in Windows 7 (and was happy to see that I can finally type function_name?and see the docstring in the Object Inspector again) but now the plotting doesn't work as it used to.

我在 Windows 7 中从 Python(x,y) 2.7.2.3 升级到2.7.6.0(很高兴看到我终于可以function_name?再次在对象检查器中输入和查看文档字符串)但现在绘图不起作用曾经。

Previously (Spyder 2.1.9, IPython 0.10.2, matplotlib 1.2.1), when I plotted this script, for instance, it would plot the subplots side-by-side in an interactive window:

以前(Spyder 2.1.9、IPython 0.10.2、matplotlib 1.2.1),例如,当我绘制此脚本时,它会在交互式窗口中并排绘制子图:

enter image description here

在此处输入图片说明

Now (Spyder 2.2.5, IPython 1.2.0, Matplotlib 1.3.1) when I try to plot things, it does the subplots as tiny inline PNGs, which is a change in IPython:

现在(Spyder 2.2.5,IPython 1.2.0,Matplotlib 1.3.1)当我尝试绘制事物时,它将子图作为微小的内联 PNG 执行,这是IPython 中的一个变化

tiny inline PNGs

微小的内联 PNG

So I went into options and found this:

所以我进入了选项并发现了这个:

graphics options

图形选项

which seems to say that I can get the old interactive plots back, with the 4 subplots displayed side-by-side, but when I switch to "Automatic", and try to plot something, it does nothing. No plots at all.

这似乎是说我可以恢复旧的交互式绘图,并排显示 4 个子绘图,但是当我切换到“自动”并尝试绘制某些内容时,它什么也没做。根本没有情节。

If I switch this drop-down to Qt, or uncheck "Activate support", it only plots the first subplot, or part of it, and then stops:

如果我将此下拉菜单切换到 Qt,或取消选中“激活支持”,它只会绘制第一个子图或其中的一部分,然后停止:

enter image description here

在此处输入图片说明

How do I get the old behavior of 4 side-by-side subplots in a single figure that I can interact with?

如何在我可以交互的单个图形中获得 4 个并排子图的旧行为?

回答by J. Martinot-Lagarde

As said in the comments, the problem lies in your script. Actually, there are 2 problems:

正如评论中所说,问题在于您的脚本。其实有2个问题:

  • There is a matplotlib error, I guess that you're passing an argument as Nonesomewhere. Maybe due to the defaultdict ?
  • You call show()after each subplot. show()should be called once at the end of your script. The alternative is to use interactive mode, look for ionin matplotlib's documentation.
  • 有一个 matplotlib 错误,我猜你在None某个地方传递了一个参数。也许是由于 defaultdict ?
  • show()在每个子图之后调用。show()应该在脚本结束时调用一次。另一种方法是使用交互模式,ion在 matplotlib 的文档中查找。

回答by Victor Arellano

Change the backend to automatic:

将后端更改为自动:

Tools > preferences > IPython console > Graphics > Graphics backend > Backend: Automatic

工具 > 首选项 > IPython 控制台 > 图形 > 图形后端 > 后端:自动

Then close and open Spyder.

然后关闭并打开 Spyder。

回答by Mike T

You can quickly control this by typing built-in magic commandsin Spyder's IPython console, which I find faster than picking these from the preferences menu. Changes take immediate effect, without needing to restart Spyder or the kernel.

您可以通过在 Spyder 的 IPython 控制台中输入内置魔法命令来快速控制它,我发现这比从首选项菜单中选择这些命令更快。更改立即生效,无需重新启动 Spyder 或内核。

To switch to "automatic" (i.e. interactive) plots, type:

要切换到“自动”(即交互式)绘图,请键入:

%matplotlib auto

then if you want to switch back to "inline", type this:

然后,如果您想切换回“内联”,请键入:

%matplotlib inline

(Note: these commands don't work in non-IPython consoles)

(注意:这些命令在非 IPython 控制台中不起作用)

See more background on this topic: Purpose of "%matplotlib inline"

查看有关此主题的更多背景信息:“%matplotlib inline”的目的

回答by Archit Pandey

After applying : Tools > preferences > Graphics > Backend > Automatic Just restart the kernelenter image description here

应用后:工具>首选项>图形>后端>自动 只需重新启动内核在此处输入图片说明

And you will surely get Interactive Plot. Happy Coding!

你一定会得到交互式情节。快乐编码!

回答by Chris

This is actually pretty easy to fix and doesn't take any coding:

这实际上很容易修复并且不需要任何编码:

1.Click on the Plots tab above the console. 2.Then at the top right corner of the plots screen click on the options button. 3.Lastly uncheck the "Mute inline plotting" button

1. 单击控制台上方的 Plots 选项卡。2.然后在绘图屏幕的右上角单击选项按钮。3.最后取消选中“静音内联绘图”按钮

Now re-run your script and your graphs should show up in the console.

现在重新运行您的脚本,您的图形应该会显示在控制台中。

Cheers.

干杯。