How to Delete Scatter Plot Points From a Figure in Matlab

It is often useful to update the location of scatter plot points overlaid on a Matlab figure, especially when displaying the incremental results of iterative algorithms. In order to do this, the existing scatter plot points need to be deleted prior to redrawing new scatter points.

In order to delete points which have been plotted on an axis with scatter(), the following code can be used:

delete(findobj(gca, 'type', 'patch'));

It is not immediately obvious from the documentation, but scatter points are considered to be a “patch” type object. Thus, this line of code finds all patch objects on the current axes and deletes them.

To delete the 2D lines drawn by Matlab’s plot() function a similar statement can be used, in this case by changing the type to the more obvious value of “line”:

delete(findobj(gca, 'type', 'line'));