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

我在 Windows 7中从 Python (x,y)2.7.2.3升级到了 2.7.6.0(很高兴看到我终于可以键入 function_name?并在对象检视器中再次看到 docstring) ,但是现在绘图不能像以前那样工作了。

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

enter image description here

现在(Spyder 2.2.5,IPython 1.2.0,Matplotlib 1.3.1) ,当我尝试绘制图形时,它将子图作为微型内联 PNG 进行绘制,这是一个 IPython 的更改:

tiny inline PNGs

所以我进入期权,发现了这个:

graphics options

这似乎是说,我可以得到旧的互动情节,与4个子情节并排显示,但当我切换到“自动”,并试图绘制一些东西,它什么也不做。完全没有阴谋。

如果我将这个下拉菜单切换到 Qt,或者取消选中“ Activate support”,它只会绘制第一个子图,或者部分子图,然后停止:

enter image description here

我如何得到4个并排子情节的旧行为在一个单一的图形,我可以互动?

304820 次浏览

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

  • There is a matplotlib error, I guess that you're passing an argument as None somewhere. 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 ion in matplotlib's documentation.

Change the backend to automatic:

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

Then close and open Spyder.

You can quickly control this by typing built-in magic commands in 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.

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)

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

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

And you will get Interactive Plot.

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

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

For most mathematical coding, I use this website and their services as they offer examples for every subject and their support is super helpful:
https://labdeck.com/application-examples-screenshots/

If your want you graph to change by a variable amount then the code you want to use is

   import matplotlib.pyplot as plt
import time
vec1=[1, 2, 3, 4, 5]
vec2py=[10, 12, 9, 11, 13]
plt.show()
axes = plt.gca()
axes.set_xlim(0, 6)
axes.set_ylim(5, 50)
plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.title('Example 1')
plt.grid()
line,= axes.plot(vec1,vec2py,color='red',lw=1)
for x in range(0,10):
vec2py = [x + 2 for x in vec2py]
line.set_ydata(vec2py)
plt.draw()
plt.pause(1e-17)
time.sleep(0.5)
plt.show()

You will have to change x for how many iterations of the graph you want and how long you want it to run for and also the +2 in the vec2py line for what variable amount you want to change it by. Naturally the code is a template and you can make any aesthetical changes. This code file is found under displaying dynamic graphs which is under python programming in the link above.

If you want to display a constant rely of information from a source, I'm not to sure how to do that but the website mention before does have an example, however it isn't in python but in a simplified form of C++.If you do want to see it then the link is https://labdeck.com/examples/dsp-ecg-processing/ecg-9-leads-graphs.pdf?01a96f&01a96f and its under ECG 9 Leads graphs in ECG on the link at the start. The graph can be shown as in a document or independently from the document.

PS this is for people who have the same question but not necessarily the same scenario as I think this will help more.