How to plot a very large number of individual points
16 views (last 30 days)
Show older comments
I have several vectors (240) each of different length.
Now for each entry of any vector I wish to plot:
1) A filled 'o' if the entry is 1
2) An empty(very lightly shaded) 'o' if the entry is 0
3) A 'x' if the entry is 2
To achieve this I have a nested for loop structure. The outer loop traverses through the vectors, and the inner loop traverses through the entries of each particular vector. Inside I have an if else block that achieves the above plotting.
The code for this is as follows:
figure_counter = figure_counter + 1;
figure(figure_counter);
hold on
% Set a max y value to start plotting
y_val = length(window_size_list);
for k = 1: length(window_size_list)
y_val = y_val - 1;
t_val = time_EWS{k};
for j = 1: length(H{k})
if H{k}(j) == 1
H_val = 1;
plot(t_val(j), y_val, 'LineStyle', 'none', 'LineWidth', 1, 'Marker', 'o', 'MarkerSize', 5, 'MarkerFaceColor', PS.Grey4, 'MarkerEdgeColor' , PS.Grey5);
elseif H{k}(j) == 2
plot(t_val(j), y_val, 'LineStyle', 'none', 'LineWidth', 1, 'Marker', 'x', 'MarkerSize', 5, 'MarkerFaceColor', PS.Red1, 'MarkerEdgeColor' , PS.Red2)
else
H_val = 0;
plot(t_val(j), y_val, 'LineStyle', 'none', 'LineWidth', 1, 'Marker', 'o', 'MarkerSize', 5, 'MarkerFaceColor', PS.Grey1, 'MarkerEdgeColor' , PS.Grey2);
end
end
end
xlabel('t');
ylabel('count');
The Problem:
The figure is very slow to respond. I am not able to set any figure properties as the property inspector just keeps loading.
What can I do to fix this issue? Am I using the wrong function to plot?
0 Comments
Accepted Answer
dpb
on 25 May 2023
Edited: dpb
on 25 May 2023
It's not the wrong function, but applying it very inefficiently. Each call to plot() produces another line object so you're creating a zillion of 'em.
Rearrange the code with logical addressing to plot all of one linestyle in one call using hold on after first to add to the same axis.
Something more like
) A filled 'o' if the entry is 1
2) An empty(very lightly shaded) 'o' if the entry is 0
3) A 'x' if the entry is 2
for i=1:numberLines
is1=(H{i}==1); % logical for case 1
is0=(H{i}==0); % logical for case 2
is2=(H{i}==2); % logical for case 3
plot(time_EWS{i}(is1),H{i}(is1),'o','MarkerSize',5,'MarkerFaceColor', PS.Grey4, 'MarkerEdgeColor', PS.Grey5);
hold on
plot(time_EWS{i}(is0),H{i}(is0),'x','MarkerSize',5,'MarkerFaceColor', PS.Red1, 'MarkerEdgeColor', PS.Red2);
plot(time_EWS{i}(is2),H{i}(is2),'o','MarkerSize',5,'MarkerFaceColor', PS.Grey1,'MarkerEdgeColor', PS.Grey2);
end
is the crux of what need to do instead.
The above can be streamlined significantly more by use of arrays to hold the various pieces such as the colors and markers and logical vectors to use a loop for the plot() call instead of multiple individual calls.
It would also be best practice to save the line handles into an array of handles to be able to use for legends or other manipulations after the initial plotting is done.
The above would plot the actual values; your code would plot a straight line -- if that is really what was wanted, then create the Y vector as Y=[N:-1:1] and use it instead with the logical indexing arrays.
0 Comments
More Answers (0)
See Also
Categories
Find more on Discrete Data Plots 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!