Create legend for plots in the first loop iteration -> clear the plot -> plot new lines without affecting the legend
3 次查看(过去 30 天)
显示 更早的评论
Hello,
I'm creating an app in App Designer where I am animating graphs by plotting the (1:index) parts of the curves in a for-loop. With more graphs it is very slow cause it needs to add new lines and update legends of the graphs every iteration.
I tried an if-condition to create legends for the first iteration (added 'AutoUpdate', 'off'), and used cla(app.AnyAxes) to clear the existing "sub-lines" at the beggining of the remaining iterations.
The problem is the legends become blank during the second iteration. If I dont use 'AutoUpdate', 'off' the legend items get renamed to data1, data2 etc. which overwrite every time. I also tried set(app.AnyAxes,'defaultLegendAutoUpdate','off') with no affect.
Is there any way to achieve "freezing" of the legend no matter what gets cleared or added to the UIAxes? Or any other reccomendation?
Thanks in advance.
2 个评论
Mario Malic
2024-4-8
I see that you want to keep the legend after deleting the plot for iterations before (why would a legend entry exist for something that's not there?). You can play with LineWidth property for the current iteration, to increase it over the previous ones, if you want to keep all entries.
Also, to speed things up, consider changing XData and YData properties of the plot for the current iteration.
回答(1 个)
Shubham
2024-4-8
Hi Jakub,
In MATLAB App Designer, managing dynamic plots and their legends, especially in animations, can be challenging due to the way MATLAB handles updates to graphical objects. Your approach of using AutoUpdate, 'off' is on the right track, but managing legends when clearing and redrawing plots requires a more nuanced handling. Here are a few strategies that could help you achieve a smoother animation without losing the legend information:
1. Manually Managing Legends
Instead of relying on automatic updates, you can manually create and update legends each time you plot. This gives you full control over the legend entries.
- Create the legend normally and turn off AutoUpdate.
- Before clearing the axes with cla(app.AnyAxes), store the legend strings and handles. After plotting the new data, recreate the legend using the stored information.
2. Using Line Objects
Another approach is to use line objects for plotting, which can be more efficient than redrawing the entire plot every iteration.
- Pre-allocate line objects for each dataset you plan to plot. For instance, if you're plotting three datasets, you create three line objects with initial data. This is done once before the loop.
hold(app.UIAxes, 'on'); % Ensure all lines are plotted on the same axes
line1 = plot(app.UIAxes, NaN, NaN, 'DisplayName', 'Dataset 1'); % Initialize with NaN
line2 = plot(app.UIAxes, NaN, NaN, 'DisplayName', 'Dataset 2');
% Add more lines as needed
legend(app.UIAxes, 'show');
hold(app.UIAxes, 'off');
- Update the XData and YData properties of each line object instead of creating new plots. This method is significantly faster and avoids issues with the legend.
for index = 1:N % N is the number of iterations
% Update line data
line1.XData = xData1(1:index);
line1.YData = yData1(1:index);
line2.XData = xData2(1:index);
line2.YData = yData2(1:index);
% Update more lines as necessary
drawnow; % Update the figure window
end
3. Freezing Legends
To "freeze" the legend despite updates to the plot:
- After the first iteration, manually create a legend with the desired entries. Use legend(app.UIAxes, {'Entry 1', 'Entry 2', ...}, 'AutoUpdate', 'off') after your plotting commands in the first iteration.
- When updating the plot, ensure you're only updating the properties of existing plot objects (as in the line objects approach) instead of creating new ones.
4. Avoid Clearing Axes
Instead of using cla(app.AnyAxes) which clears the axes and necessitates redrawing everything (including legends):
- Update existing plots directly as suggested above.
- If you must clear, save the legend entries before clearing and manually set them again after plotting
The key to smooth animations in MATLAB, especially with App Designer, lies in efficiently updating existing graphics objects rather than redrawing them entirely. The line objects method is particularly effective for animations and dynamic plots and can significantly improve performance while maintaining control over the legend.
I hope this helps!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!