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'));
1 comment
"Why delete(fin?dobj(gca,'?type', 'patch')) is not working for erasing scattered points ?"
Because s = scatter(___) produces a scatter object, not a patch object.
Instead, use
delete(findobj(gca,'type', 'scatter'))
Or better yet, use the scatter output which does not require the use of the axis handle and is more responsible since you're deleting a specific object rather than deleting all existing matches to an object type within the current axes.
s = scatter(. . .);
delete(s)
Comments are closed.