Legend of subplot duplicates entry after for loop

7 vues (au cours des 30 derniers jours)
Alejandro Puertolas
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

Réponse acceptée

Walter Roberson
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
Alejandro Puertolas
Alejandro Puertolas le 30 Juil 2022
I tried writing it like this:
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', '-');
hold(System_Handle.Cache_DynamicVariables.child_handles(1,1), 'on' );
end
end
legend(System_Handle.Cache_DynamicVariables.child_handles(1,1),'Location','best');
hold(System_Handle.Cache_DynamicVariables.child_handles(1,1), 'off' );
This does solve the second problem and lets hold off take effect deleting the lines from previous lines. But this also ignores all the item setting because the first plot does cla.
Is there a way of preventing the settings to be ignored? Should i write the setting after the plotting loop?
Alejandro Puertolas
Alejandro Puertolas le 30 Juil 2022
Putting the setting after the loop gives the behaiviour i wanted, thank you very much for the insight on how hold on and hold off works.
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', '-');
hold(System_Handle.Cache_DynamicVariables.child_handles(1,1), 'on' );
end
end
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);
legend(System_Handle.Cache_DynamicVariables.child_handles(1,1),'Location','best');
hold(System_Handle.Cache_DynamicVariables.child_handles(1,1), 'off' );

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by