Reputation: 455
I am using a matplotlib figure embedded in a WxPython GUI to present some data. The content of the figure (data displayed) changes constantly in function of the buttons clicked, ...
The data are of two types.
1) contour lines
self.axes.contour(x_scale_map,y_scale_map,matrix,cl,cmap=my_cmap,extent=0,matrix.shape[1]-1,0,matrix.shape[0]-1))
This is relatively slow to load (~1s), but does not change very often.
2) On top of this contour, I plot for instance some lines
self.axes.axhline(y,color='black')
These lines are obviously drawn instantly and change often in function of what the user clicks. In these situations, previously drawn lines need to disappear and new ones need to appear, while the contour map stays unchanged.
Now, my problem is as follows. I have not found a way to remove only the lines and not the contour. The only way to obtain the desired result seems to be doing:
self.axes.clear()
and then replot both the contour and the new lines. But as mentioned, reloading the contour each time is slow and thus annoying.
Is there a way to clear only the lines from the figure? I have tried to use superimposed subplots by doing something like:
self.axes1 = self.fig.add_subplot(111)
self.axes2 = self.fig.add_subplot(111)
self.axes1.contour(...)
self.axes2.axhline(y,color='black')
self.axes2.clear()
but this last line clears the entire figure.
Does anyone know how to achieve the desired functionality? Thanks
Upvotes: 7
Views: 10447
Reputation: 41
I had success with a problem like this by using the equivalent of self.axes2.cla() to just clear the specified axes. In my case, I had to use sharex and sharey along with self.axes2.patch.set_aplha(0.0) to make the facecolor transparent and see through to the plot below.
My issue now though is I had a picker defined on the axes1 plot and it does not seem to trigger with the overlying plot present.
Upvotes: 1
Reputation: 455
The following Q&A gives the solution to this problem.
In other words, to be able to remove a line from the figure:
1) keep track of the line by storing its reference when drawing it:
my_line = self.axes.axhline(y,color='black')
2) the removal is then done as follows:
my_line.remove()
del my_line
Upvotes: 7