# appending data to scatter(x,y)

70 views (last 30 days)
cgo on 8 Oct 2015
Edited: Will Reeves on 16 Mar 2022
I am n random points in two dimensional space (x,y). I am using scatter(x,y) to visualize them.
Now I want to take the center of mass of these points (x,y) with a formula, and now I want to append them to the scatter(x,y). There are now n+1 points. How do I do this, aside from using 'hold on'?

Robert Dylans on 8 Oct 2015
"Center of mass" makes me hesitate a bit on this. Might want to clarify exactly what you mean by that. Do you want to connect the points in order, forming a closed shape, and calculate centroid from that? Or just use averages to find the center of the data points themselves? Or find the center of the outer bounds of the data, directly between min&max?
Assuming the second option above:
x=[0,1,1.5,3,5];
y=[5,3,1.5,1,0];
scatter(x,y);
c=[mean(x),mean(y)];
hold on;
scatter(c(1),c(2));
hold off;
This only appends the center (c) to the plot, not to the data (x,y). To do that,
x=[x c(1)];
y=[y c(2)];

Will Reeves on 16 Mar 2022
Edited: Will Reeves on 16 Mar 2022
I would like to add scatter points to a plot during a loop to visualise the process I'm running.
I currently use hold on, and scatter the new point every time it's created, or destroyed. Unfortunately, the way Matlab handles (excuse the pun) scatter plots is fine if you are scattering a large number of points at once. If you're scattering one point at a time it eats memory like a monster if you have a few thousand individual scatter points. (I got to 20GB quickly then my PC ground to a stop!).
It would be lovely to have a feature where you can add data to a dataset or scatter plot and have it "add" the point, rather than creating a whole new object. For instance
sp=scatter(data(:,1),data(:,2));
then add a point to the data, then
sp.update
or something?
Of course this leads to another issue in Matlab whereby "adding extra points to arrays" is horrifically slow, and pre-allocation is fine if you already know the outcome. It would be lovely to have the "list functionality" that other languages have where you can efficiently add data to a list without reallocation - obviously a list object wouldn't be as fast at other types of computation though appreciated.