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