实时轨迹绘制 - Matplotlib

4
我希望使用matplotlib绘制轨迹。在我编写的程序的每次迭代中,我会获得物体的x和y坐标。我想在xy图上绘制该物体的运动轨迹。我使用了以下代码:
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
import time

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(-100,100)
ax.set_ylim(-100,100)
plt.ion()
plt.show(block=True)

verts = [
    (0, 0), #I'm just assuming two sets of points here. I actually intend to put variables here which I can update in real time.
    (27, 0)
    ]

codes = [Path.MOVETO,
         Path.LINETO]  

path = Path(verts, codes)

#fig = plt.figure()
#ax = fig.add_subplot(111)
patch = patches.PathPatch(path, facecolor='white', lw=2)
ax.add_patch(patch)
#ax.set_xlim(-100,100)
#ax.set_ylim(-100,100)
plt.draw()
time.sleep(1)

但是我只能看到一个带有两个轴的空白窗口。是的,我确实改变了代码的顺序以适应我的需求(请参见注释行),因为对于实时操作,我需要将其放入循环中。有人能帮我解决这个问题吗?另外,如果我不使用“patch”,线条会变得不可见。有其他的解决方法吗?

1个回答

6

谢谢,我解决了自己的问题。使用plt.show()替代plt.show(block=True)。此外,在代码结尾添加plt.pause(0.05)。time.sleep()是不必要的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接