How do I speed up plotting of interactable scatter points?
Show older comments
Hello,
I have the following code:
for ii = 1:length(tempSpikes)
p(ii) = plot(app.Trace, tempSpikes(ii)*app.msConvert, app.xi(app.m.mainCh,tempSpikes(ii)),'k*');
end
set(p, 'ButtonDownFcn', {@pointSelected, app.Trace, p})
set(app.Trace,'UserData', []);
This plot loop plots 100s to 1000s of individual markers that can be clicked on using the callback pointselected.
pointselected is a function that just flags which markers have been selected/deselected.
This plotting process takes a long time (>10 sec), and using figure tools like zoom and pan are very slow also.
Is there a way to speed this up? I tried replacing the plot command with scatter and that was 3 times as slow.
Thanks!
Edit: The plot I have has the xaxis range from 0 to 2e5 while the yaxis ranges from -1000 to 1000 at a aspect ratio of 2:1. So using ginput and distance finding is not ideal as the distance in the xaxis is much greater per pixel causing unexpected points to be chosen if the mouse is slightly off the point.
Also ideally I would like the points to be clickable and unclickable infinitely until I press a button to act on the data. I may be zooming in and out between clicking points and clicking other buttons on the GUI. ginput locks me into point collection mode.
Accepted Answer
More Answers (4)
darova
on 23 Aug 2019
Creates handler for each point
x = rand(3,1);
y = rand(3,1);
h = plot([x x*0]',[y y*0]','.r');
Select points
x = rand(10,1);
y = rand(10,1);
plot(x,y,'.r')
g = ginput(3);
D = pdist2([x y],g);
[~,ix] = min(D);
hold on
plot(x(ix),y(ix),'or')
hold off
Yair Altman
on 23 Aug 2019
Try to use the vectorized version of plot to plot all the data points in a single function call rather than a loop, and also try using a simpler marker object (e.g. '+' rather than '*'):
p = plot(app.Trace, tempSpikes*app.msConvert, app.xi(app.m.mainCh,tempSpikes), 'k+');
You can also try to replace the plot function with the lower-level line function, which is faster than either plot or scatter (example).
Alternatively, you could possibly also use the scatter function, which has an undocumented speedup option, which is quite old so might not work in your case, but it's worth a try (link).
1 Comment
Daniel Ko
on 23 Aug 2019
Yair Altman
on 23 Aug 2019
Indeed - you get a single line object with multiple data points, this is the main reason for the speedup. In your callback you can determine which data-point was clicked by checking the click-location's proximity to the data points, something similar to the following:
hAxes = gca;
clickedPos = get(hAxes,'CurrentPoint');
xSize = diff(hAxes.XLim);
ySize = diff(hAxes.YLim);
% compute distance of clicked point from all data points
dist = hAxes.PlotBoxAspectRatio(1)^2 * ((clickedPos(1,1)-p.XData)./xSize).^2 ...
+ hAxes.PlotBoxAspectRatio(2)^2 * ((clickedPos(1,2)-p.YData)./ySize).^2;
% select the data point which is relatively closest to the clicked point
[minDist, dataPointIdx] = min(dist);
MOHAMMED BELAL
on 11 Jan 2020
0 votes
hello how i can plot Sobel,Prewitt,Laplacian Edge Detectors performance and process time for comparison.
Any help ?
thank you.
Categories
Find more on Data Exploration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!