So, I would like to know if there is a way to delete an line already plotted using matplotlib. But here is the thing:
I'm plotting within a for loop, and I would like to check before plotting if the line that I'm about to draw is already on the figure, and if so, I would like to delete it.
I was told an idea about plotting it anyways but with the same color of the background, but again, to do this I would have to check if the line already exists. Any idea how to do this?
CodePudding user response:
Any idea how to do this?
- During iteration, before making a new line
- check if x and y coordinates of the new line are the same as any of the lines already contained in the plot's Axes..
CodePudding user response:
Following the comment of @wii, I wrote this function which worked perfectly
def check_repeated_lines(x,y,ax,colors):
lines = ax.get_lines()
bol = False
for i in range(len(lines)):
x_i = lines[i].get_data()[0]
y_i = lines[i].get_data()[1]
if array_equal(x_i,x) == True and array_equal(y_i,y) == True:
bol = True
break
else:
pass
if bol == True:
ax.lines.remove(lines[i])
else:
ax.plot(x,y, linestyle = '-', color = colors, linewidth = 0.5)