我试图在模拟的每个时间步之后更新matplotlib中的数字。
我设法做到了这一点:
import time, math
import matplotlib
matplotlib.use(u'Qt4Agg') # use some backend
# (c.f. http://stackoverflow.com/a/21836184 for .use())
import matplotlib.pyplot as plt
plt.ion()
fig = plt.figure()
sub = fig.add_subplot(1, 1, 1)
x = [i/100. for i in range(-500, 500)]
sin = lambda x : [math.sin(i) for i in x]
axis, = sub.plot(x, sin(x), 'r')
shift = 0.1
while shift < 5:
shift += 0.01
x = [i+shift for i in x]
axis.set_ydata(sin(x))
plt.pause(0.0001) # was plt.draw()
在提出这个解决方案的过程中,我几乎生气,因为Qt4Agg
后端现在可以正常使用draw()
命令,现在有一个plt.pause(0.0001)
命令。 / p>
现在可行,但我收到以下警告:
``/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py:2399: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented
warnings.warn(str, mplDeprecation)``
我也试过GTK3Agg
后端,但我不确定我们想要运行项目的计算机是否支持它。最令人惊讶的是,这个后端与plt.draw()
一起使用(除了窗口冻结的事实)但在使用plt.pause(0.0001)
运行它时会抛出以下警告和错误:
/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py:2399: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented
warnings.warn(str, mplDeprecation)
Traceback (most recent call last):
File "subplot_demo.py", line 24, in <module>
plt.pause(0.0001) # was plt.draw()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 199, in pause
canvas.start_event_loop(interval)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_gtk3.py", line 370, in start_event_loop
FigureCanvasBase.start_event_loop_default(self,timeout)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 2407, in start_event_loop_default
self.flush_events()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_gtk3.py", line 365, in flush_events
Gtk.main_iteration(True)
File "/usr/lib/python2.7/dist-packages/gi/types.py", line 43, in function
return info.invoke(*args, **kwargs)
TypeError: main_iteration() takes exactly 0 arguments (1 given)
我现在假设pause()
已被弃用(因为它也很难找到一些有用的文档),但仍然坚持使用Qt4Agg
后端,因为我确信将安装PyQt4在目标系统上。
我现在想知道:我如何正确draw()
使用Qt4Agg
? draw()
电话无效,pause()
已弃用 - 我该怎么办? (旁注:pause()
- GTK3Agg
后端的调用究竟出了什么问题?)
关于版本的一些注释:
Ubuntu: 12.04, Matplotlib: 1.4.3, Python: 2.7.3, GCC 4.6.3, PyQt: 4.8.1