Legend of subplot duplicates entry after for loop
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Alejandro Puertolas
le 30 Juil 2022
Commenté : Alejandro Puertolas
le 30 Juil 2022
I have this code for some plots that are created in a for loop. this "Real_Time_Plots" function gets called for every time step of the simulation. The problem i am having is that as it is every time the function gets called, the legend of the subplot gets duplicated (Example: First function call the legend has the following entries: Pr1, Pr2, Pr3. On the Second call the legend is double the length with following entries: Pr1, Pr2, Pr3, Pr1, Pr2, Pr3 eventho no new lines were added to the plot ). Am i writing the "hold on's" and "hold off's" at the wrong place?
function Real_Time_Plots(System_Handle, TimeStep_Count )
%Subplots get created
if (isfield(System_Handle.Cache_DynamicVariables,'f1'))
elseif ~(isfield(System_Handle.Cache_DynamicVariables,'f1'))
System_Handle.Cache_DynamicVariables.f1 = figure(1);
ax1 = subplot( 2,3,1 );
ax2 = subplot( 2,3,2 );
ax3 = subplot( 2,3,3 );
ax4 = subplot( 2,3,4 );
ax5 = subplot( 2,3,5 );
ax6 = subplot( 2,3,6 );
System_Handle.Cache_DynamicVariables.child_handles = get(System_Handle.Cache_DynamicVariables.f1,'Children');
else
end
%Name and color array initialization
precipitates = size(System_Handle.Precipitates, 1);
names = cell(precipitates,1);
cc = jet(precipitates);
for i=1:1:precipitates
names{i}=strcat('Pr', num2str(i));
end
%First Subplot
xlim(System_Handle.Cache_DynamicVariables.child_handles(1,1), [0, t_end]);
xlabel(System_Handle.Cache_DynamicVariables.child_handles(1,1), 'time in s' );
ylabel(System_Handle.Cache_DynamicVariables.child_handles(1,1), 'mean radius in m' );
set(System_Handle.Cache_DynamicVariables.child_handles(1,1),'XScale','log');
set(System_Handle.Cache_DynamicVariables.child_handles(1,1),'YScale','log');
set(System_Handle.Cache_DynamicVariables.child_handles(1,1),'FontSize',16);
hold(System_Handle.Cache_DynamicVariables.child_handles(1,1), 'on' );
for i=1:1:precipitates
if System_Handle.Precipitates{i, 1}.Physical_Properties.IsClusterPhaseType
plot(System_Handle.Cache_DynamicVariables.child_handles(1,1), ...
... Time(1:TimeStep_Count,1), MeanRadius.DataTable(1:TimeStep_Count,3 ), 'DisplayName', names{i} , 'color', cc(i,:) , 'LineStyle', '-');
end
end
legend(System_Handle.Cache_DynamicVariables.child_handles(1,1),'Location','best');
hold(System_Handle.Cache_DynamicVariables.child_handles(1,1), 'off' );
%Code for the rest of subplots
drawnow
end
0 commentaires
Réponse acceptée
Walter Roberson
le 30 Juil 2022
setting xlim and fontsize and the other items do not cla (clear axes).
You then hold on, draw, hold off.
Next cycle you re-do setting the limits and so on, none of which removes anything from the axes. You then hold on... which tells matlab not to remove any of the lines from the previous cycle. You add more lines, hold off..
Using hold off does not erase anything: it just sets the conditions so that the next command that draws a line or surface or patch or image, will automatically trigger clearing the axes. But when you turn hold on before the next round of drawing....
3 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Legend dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!