
Increase the plotting performance in datacursormode
4 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to plot the figure with many possible architectures in a graph. However, beacuase of having millions of architectures, plotting is not at all efficient.
Before introducing the datacursormode for getting the details of the points, plotted into the figure, the graph was smoothly generated even for 115 millions of architectures. But after bringing the datacursormode, it is not even working for 354295 number of architectures.
Here my code is:
%Plotting the tradespace and pareto frontier with corresponding architectures
Utopia = UtilityHighRange; % for plotting purposes add Utopia point
plot(archs_cost(1),archs_util(1),'b.','Tag',sprintf('ix %d\n%s',1, join(archs(1,:))) )
hold on % moved up from below
for n = 2:n_arch
plot(archs_cost(n),archs_util(n),'b.','Tag',sprintf('ix %d\n%s',1, join(archs(n,:))) )
end
datacursormode on
dcm = datacursormode(gcf);
set(dcm,'UpdateFcn',@myupdatefcn)
xlabel('Cost')
ylabel('Utility')
xlim([CostLowerRange CostHighRange])
ylim([UtilityLowerRange UtilityHighRange])
title('Trade Space Pareto')
hold on
plot(Utopia,'pg','LineWidth',5)
annotation('textarrow',[0.1657 0.1357],[0.8738 0.9238],'String','Utopia')
plot(pareto_frontier(1, 2), pareto_frontier(1, 3), 'rs', 'LineWidth', 3, 'Tag', sprintf('ix %d\n%s', 1, join(pareto_frontier_archs(1, :))))
hold on
for n = 2:frontier_idx
plot(pareto_frontier(n:frontier_idx, 2), pareto_frontier(n:frontier_idx,3), 'rs', 'LineWidth', 3, 'Tag', sprintf('ix %d\n%s', 1, join(pareto_frontier_archs(n, :))))
end
datacursormode on
dcm = datacursormode(gcf);
set(dcm, 'UpdateFcn', @myupdatefcn)
hold off
in above code, n_arch = 354294 and it is flexible to increase and decrease. And it can reach to its highest capacity of 115 millions.
Now, when with datacursormode on, I am running this code, the figure is being plotted but in the distorted manner. Upto some extent, it works and after some time, Matlab becomes unresponsive and then I need to restart the application everytime.
It would be really appreciated if anyone of you could help me sort this issue and improve the performance of the plotting the data in the code. It is really very necessary as I ended up several times with my computer getting restart although having very high configuration(RAM 16 GB).
Thank you.
0 个评论
回答(1 个)
Raag
2025-4-27
Hi Kaivlya,
It is my understanding that the performance issue occurs because the code creates a separate plot object for each data point using a loop with individual plot calls. When using ‘datacursormode’, MATLAB attempts to manage a data tip for every single plot object, which can lead to significant slowdowns, high memory usage, and can cause MATLAB to become unresponsive, especially with hundreds of thousands or millions of points.
To improve performance, you can plot all points in a single call instead of looping, store any additional data (such as architecture details) in the ‘UserData’ property of the plot object and customize the data tip using a custom ‘UpdateFcn’ that accesses this data.
Below is an example how you can modify your code:
n_arch = 100000;
archs_cost = rand(1, n_arch) * 100;
archs_util = rand(1, n_arch) * 100;
archs = string(randi(9, n_arch, 5));
% Plot all points at once
h = plot(archs_cost, archs_util, 'b.');
h.UserData = archs;
datacursormode on
dcm = datacursormode(gcf);
set(dcm, 'UpdateFcn', @myupdatefcn);
function txt = myupdatefcn(~, event_obj)
pos = event_obj.Position;
idx = event_obj.DataIndex;
archs = event_obj.Target.UserData;
arch_str = char(join(archs(idx, :), " "));
txt = {['X: ', num2str(pos(1))], ...
['Y: ', num2str(pos(2))], ...
['Arch: ', arch_str]};
end
This approach creates only one plot object, which is much more efficient, the data cursor will display the correct information for each point and should work smoothly even for very large datasets as shown below:

0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!