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
The close() function in pyplot module of matplotlib library is used to close a figure window.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With