Python 在 Spyder IDE 中使用 Matplotlib 绘制内联或单独的窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29356269/
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
Plot inline or a separate window using Matplotlib in Spyder IDE
提问by pyan
When I use Matplotlib to plot some graphs, it is usually fine for the default inline drawing. However, when I draw some 3D graphs, I'd like to have them in a separate window so that interactions like rotation can be enabled. Can I configure in Python code which figure to display inline and which one to display in a new window?
当我使用 Matplotlib 绘制一些图形时,对于默认的内联绘图通常很好。但是,当我绘制一些 3D 图形时,我希望将它们放在一个单独的窗口中,以便可以启用旋转等交互。我可以在 Python 代码中配置哪个图形显示内联,哪个图形显示在新窗口中?
I know that in Spyder, click Tools, Preferences, Ipython Console, Graphics and under Graphics Backend select “automatic” instead of “inline”. However, this make all the figures to be in new windows. It can be messy when I have a lot of plots. So I want only those 3D plot to be in new windows, but all the other 2D plots remain inline. Is it possible at all?
我知道在 Spyder 中,单击“工具”、“首选项”、“Ipython 控制台”、“图形”,然后在“图形后端”下选择“自动”而不是“内联”。但是,这会使所有数字都在新窗口中。当我有很多情节时,它可能会很混乱。所以我只希望那些 3D 绘图出现在新窗口中,但所有其他 2D 绘图保持内联。有可能吗?
Thanks!
谢谢!
采纳答案by Stefano Fedele
type
类型
%matplotlib qt
when you want graphs in a separate window and
当您想要在单独的窗口中显示图形时
%matplotlib inline
when you want an inline plot
当你想要一个内联图时
回答by Don Ernesto
Magic commands such as
魔法命令,例如
%matplotlib qt
work in the iPython console and Notebook, but do not work within a script.
在 iPython 控制台和 Notebook 中工作,但不在脚本中工作。
In that case, after importing:
在这种情况下,导入后:
from IPython import get_ipython
use:
用:
get_ipython().run_line_magic('matplotlib', 'inline')
for inline plotting of the following code, and
用于以下代码的内联绘图,以及
get_ipython().run_line_magic('matplotlib', 'qt')
for plotting in an external window.
用于在外部窗口中绘图。
Edit: solution above does not always work, depending on your OS/Spyder version Anaconda issue on GitHub. Setting the Graphics Backend to Automatic (as indicated in another answer: Tools >> Preferences >> IPython console >> Graphics --> Automatic) solves the problem for me.
编辑:上述解决方案并不总是有效,这取决于您在 GitHub上的 OS/Spyder 版本 Anaconda 问题。将图形后端设置为自动(如另一个答案所示:工具 >> 首选项 >> IPython 控制台 >> 图形 --> 自动)为我解决了这个问题。
Then, after a Console restart, one can switch between Inline and External plot windows using the get_ipython() command, without having to restart the console.
然后,在控制台重新启动后,可以使用 get_ipython() 命令在内联和外部绘图窗口之间切换,而无需重新启动控制台。
回答by AuHgNeM
Go to Tools >> Preferences >> IPython console >> Graphics >> Backend:Inline, change "Inline" to "Automatic", click "OK"
转到工具>>首选项>>IPython控制台>>图形>>后端:内联,将“内联”更改为“自动”,单击“确定”
Reset the kernel at the console, and the plot will appear in a separate window
在控制台重置内核,绘图将出现在单独的窗口中