绘制线条而不阻塞执行

发布于 2024-08-22 15:18:59 字数 251 浏览 1 评论 0原文

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

当我使用命令 show() 绘制图表时,我的代码会阻塞在该命令处。

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

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

I am using matplotlib to draw charts and graphs.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

佼人 2024-08-29 15:18:59

在以 -pylab 开头的 IPython 中,它不应该阻塞。

否则:
使用 ion() 您可以打开交互模式。 show() 不会阻塞您的系统
不再了。每个 draw()plot(x, y) 都会更新您的绘图。

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

另请参阅:http://www.scipy.org/Cookbook/Matplotlib/Animations

In IPython started with -pylab it should not block.

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.

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

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

老旧海报 2024-08-29 15:18:59

如果您不使用 IPython shell 而是运行程序,您可能想要执行以下操作:

pyplot.draw()

plot() 之后,可能后面跟着,

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

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

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

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

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

pyplot.draw()

after a plot(), possibly followed by

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.show() is actually an infinite loop that handles events in the main plotting window (such as zooming, panning, etc.).

何处潇湘 2024-08-29 15:18:59

在 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.

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:

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.
寂寞陪衬 2024-08-29 15:18:59

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文