Reputation: 20345
I have run 3 Python scripts and each of them generated one curve in the plot.
Each curve is made up of hundreds of small line segments.
Thus, each curve is drawn by a series of plot()
instead of one.
But all these plot()
share the same parameters (e.g. the color, style of one curve is consistent).
Thus, I think it is still possible to easily remove all the segments drawn by particular one script.
I find that the curve generated by the most-recently-run script is erroneous. Therefore, I wish to remove it. But at the same time, I cannot afford to just close the window and redraw everything. I wish to keep all the other curves there.
How may I do that?
Updates: Plotting Codes
for i, position in enumerate(positions):
if i == 0:
plt.plot([0,0], [0,0], color=COLOR, label=LABEL)
else:
plt.plot([positions[i - 1][0], position[0]], [positions[i - 1][1], position[1]], STYLE, color=COLOR)
#plt.plot([min(np.array(positions)[:,0]), max(np.array(positions)[:,0])], [0,0], color='k', label='East') # West-East
#plt.plot([0,0], [min(np.array(positions)[:,1]), max(np.array(positions)[:,1])], color='k', label='North') # South-North
plt.gca().set_aspect('equal', adjustable='box')
plt.title('Circle Through the Lobby 3 times', fontsize=18)
plt.xlabel('x (m)', fontsize=16)
plt.ylabel('y (m)', fontsize=16)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.draw()
Upvotes: 1
Views: 6858
Reputation: 87356
I think your entire loop could be replaced by:
pos = np.vstack(positions) # turn your Nx2 nested list -> Nx2 np.ndarray
x, y = pos.T # take the transpose so 2xN then unpack into x and y
ln, = plt.plot(x, y, STYLE, color=COLOR, label=LABEL)
Note the ,
it is important and unpacks the list that plot
returns.
If you want to remove this line, just do
ln.remove() # remove the artist
plt.draw() # force a re-rendering of the canvas (figure) to reflect removal
I can't tell if you use of positions[-1]
is intentional or not, but if you want to force it to be periodic, do
pos = np.vstack(positions + positions[:1])
If you really want to plot each segment as a sepreate line, use LineCollection
, see https://stackoverflow.com/a/17241345/380231 for an example
Upvotes: 3