擅长:python、mysql、java
<blockquote>
<p><em>Is there a way in which I can update the plot just by adding more point[s] to it...</em></p>
</blockquote>
<p>在matplotlib中,有多种设置数据动画的方法,具体取决于您拥有的版本。你看过这些例子吗?另外,请查看matplotlib文档中更现代的<a href="http://matplotlib.sourceforge.net/examples/animation/index.html">animation examples</a>。最后,<a href="http://matplotlib.sourceforge.net/api/animation_api.html">animation API</a>定义了一个函数<a href="http://matplotlib.sourceforge.net/api/animation_api.html#matplotlib.animation.FuncAnimation">FuncAnimation</a>,它可以及时地为函数设置动画。这个函数可能只是用来获取数据的函数。</p>
<p>每个方法基本上都设置要绘制的对象的<code>data</code>属性,因此不需要清除屏幕或图形。可以简单地扩展<code>data</code>属性,这样您就可以保留前面的点,并继续添加到您的线(或图像或您正在绘制的任何内容)中。</p>
<p>假设您说您的数据到达时间不确定,那么您最好的选择可能是执行以下操作:</p>
<pre><code>import matplotlib.pyplot as plt
import numpy
hl, = plt.plot([], [])
def update_line(hl, new_data):
hl.set_xdata(numpy.append(hl.get_xdata(), new_data))
hl.set_ydata(numpy.append(hl.get_ydata(), new_data))
plt.draw()
</code></pre>
<p>然后,当您从串行端口接收数据时,只需调用<code>update_line</code>。</p>