本文介绍了实时轨迹绘制 - Matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 matplotlib 绘制轨迹.在我编写的程序的每次迭代中,我都获得对象的x和y坐标.我想在xy图上绘制该对象的运动.我使用了以下代码:

I want to plot a trajectory using matplotlib. In each iteration of a program I wrote, I obtain the x and y coordinates of an object. I would like to draw the movement of this object on an xy graph. I used the following code:

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)

但是我只能看到一个带有两个轴的空白窗口.是的,我确实更改了代码顺序以适合我的需要(请参见注释行),因为对于实时而言,我需要将其循环放置.有人可以帮我从这里出去吗?另外,如果我不使用补丁",线条就会变得不可见.还有另一种方法吗?

But all I can see is a blank window with two axes. Yes, I did change the order of the codes to suit my needs (see commented lines) since for real time, I would need to put this in a loop. Can anyone help me out here? Also, if I don't use "patch", the lines become invisible. Is there another way around?

推荐答案

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

Thanks, I solved my own problem. Use plt.show() instead of plt.show(block=True). Also, add plt.pause(0.05) to the end of the code. time.sleep() is unnecessary.

这篇关于实时轨迹绘制 - Matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:22