python 在不阻塞执行的情况下绘制线条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2310851/
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-11-04 00:20:46  来源:igfitidea点击:

plotting lines without blocking execution

pythonmatplotlib

提问by Abruzzo Forte e Gentile

I am using matplotlib to draw charts and graphs.

我正在使用 matplotlib 绘制图表和图形。

When I plot the chart using the command show()my code blocks at this command.

当我使用命令绘制图表时,show()我的代码块在此命令处。

I would like to refresh my list of values with new data , and than refresh the image on the background. How to do that without closing each time the window with the graph? Below is the code I am using

我想用新数据刷新我的值列表,而不是刷新背景上的图像。如何在不每次关闭带有图形的窗口的情况下做到这一点?下面是我正在使用的代码

import pylab
a = (1,2,3,4)
pylab.plot(a)
pylab.show() # blocks here

采纳答案by Stefan

In IPython started with -pylabit should not block.

在 IPython 中,-pylab它不应该阻塞。

Otherwise: With ion()you turn the interactive mode on. show()does not block your system anymore. Every draw()or plot(x, y)updated your plot.

否则:ion()打开交互模式。show()不再阻止您的系统。每个draw()plot(x, y)更新你的情节。

ioff()turns interactive mode off. Useful if you add lots of data and don't want to update every little detail.

ioff()关闭交互模式。如果您添加大量数据并且不想更新每个小细节,则很有用。

See also: http://www.scipy.org/Cookbook/Matplotlib/Animations

另见:http: //www.scipy.org/Cookbook/Matplotlib/Animations

回答by Eric O Lebigot

If you are not using the IPython shell but instead running a program, you probably want to do:

如果你不使用 IPython shell 而是运行一个程序,你可能想要这样做:

pyplot.draw()

after a plot(), possibly followed by

在 a 之后plot(),可能跟在

raw_input("Press enter when done...")

so as to wait for the user before plotting something else.

以便在绘制其他内容之前等待用户。

If you do pyplot.ion()at the beginning of your program, doing draw()can often even be skipped.

如果您pyplot.ion()在程序的开头执行,draw()则通常甚至可以跳过执行。

pyplot.show()is actually an infinite loop that handles events in the main plotting window (such as zooming, panning, etc.).

pyplot.show()实际上是一个无限循环,用于处理主绘图窗口中的事件(例如缩放、平移等)。

回答by lkiraly

On MacOS X i had the problem that unblocking only produced a white screen. In the end @tyleha's suggestion using %pylab directly in the note book helped. In fact it's suggested when using the deprecated the -pylab flag:

在 MacOS X 上,我遇到了解锁只会产生白屏的问题。最后@tyleha 的建议直接在笔记本中使用 %pylab 有所帮助。实际上,建议在使用已弃用的 -pylab 标志时:

bash:~/Projects/plyground $ python -m IPython notebook -pylab
WARNING: `-pylab` flag has been deprecated.
Use `--matplotlib <backend>` and import pylab manually.
[E 21:09:05.446 NotebookApp] Support for specifying --pylab on the command line has been removed.
[E 21:09:05.447 NotebookApp] Please use `%pylab` or `%matplotlib` in the notebook itself.

回答by Olivier Verdier

This works by invoking Ipython with the -wthread(or the -pylab) option. It will not block on showanymore.

这是通过使用-wthread(或-pylab)选项调用 Ipython 来实现的。它不会再阻塞show了。