1

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()
Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
  • **Please include your code**. As of right now, all we can do is guess how you are plotting your data. – sodd Aug 06 '13 at 09:06
  • some example data for positions would also help. WHy do you iterate over the points to plot them though. matplotlib has lots of functionality that, except probably if you want each line segment to have different parameters which does not seem to be what your code is doing. Depending on which interface/environment you are using to run script you might need a plt.clf() before you start in your script. – Joop Aug 06 '13 at 10:16
  • @Joop No, actually their parameters are all the same. The only reason why I am doing the iteration is that I am a newbie to Python. So still unclear how to do it purely in Python. COuld you please kindly give some suggestion? like plotting the whole curve with one line of code – Sibbs Gambling Aug 06 '13 at 15:56
  • matplotlib has got a great website with examples that are very powerfull. Simple example: http://matplotlib.org/examples/lines_bars_and_markers/line_demo_dash_control.html Check out the rest of the examples as well all of them have working code attached. – Joop Aug 07 '13 at 06:30
  • even simpler.. but you will have to get comfortable with another package is using pandas. check out http://pandas.pydata.org/pandas-docs/stable/visualization.html. Pandas uses matplotlib as backend for its plotting, but normally has some pretty good defaults. – Joop Aug 07 '13 at 06:33

1 Answers1

3

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

Community
  • 1
  • 1
tacaswell
  • 84,579
  • 22
  • 210
  • 199