Hold on issue for subplots
147 次查看(过去 30 天)
显示 更早的评论
Hello,
I had created 3 figures consisting of subplots: 4x2; 6x2 and 4x2 and saved them in 3 seperate .fig files, no problem on this step. The thing is, I need to use those saved figures again and add other plots without deleting the existing ones (directly on existing data, imagine you have a sine wave on plot 4,2,1 and then you are adding a cosine wave; now you have 2 waves inside this plot). For the figures sized 4x2; I do not have any issues but for the figure sized 6x2, whenever I try to add another graph, the existing one goes away. Actually all the settings are going away (title, axes names, etc...) and the new graphs are added on a blank subplot. Basically "hold on" does not work even I did everything the same as I did for the other figures. I researched about this issue and it is mentioned that "SUBPLOT clears the axes when called unless the new subplot properties (such as 'position') match the original subplot properties". Eventually I tried to create the new figure with the exact same graphs and properties, the issue persists. The piece of code that works on the first figure but not for the second is: (By the way I am using Matlab 2013b, so not every function is available for me...)
numX = open(name_figX_final);
figure(numX); pause(2); hold on
The code in summary:
while(a<=length(name_signals_array))
if a==1
num1 = open(name_fig1_final);
figure(num1); pause(2); hold on
subplot(421),
plot(wind_list, signals_stats(1,:), '--ro'); set(gca,'XTick',wind_label); hold on
plot(wind_list, signals_stats(2,:), '--r^'); plot(wind_list, signals_stats(3,:), '--rv');
title('Signals421'); xlabel(prop_xlabel); ylabel('rpm'); legend({'min:','max:', 'mean:'},'Location','best')
subplot(422),
plot(wind_list, signals_stats(4,:), '--ro'); % set(gca,'XTick',wind_label);
title('Signals421'); xlabel(prop_xlabel); ylabel('rpm'); legend({'std:'},'Location','best')
elseif a==2
subplot(423);
plot(wind_list, signals_stats(5,:), '--bo'); set(gca,'XTick',wind_label); hold on
plot(wind_list, signals_stats(6,:), '--b^'); plot(wind_list, signals_stats(7,:), '--bv');
title('Signals423'); xlabel(prop_xlabel); ylabel('deg'); legend({'min:','max:', 'mean:'},'Location','best')
subplot(424),
plot(wind_list, signals_stats(8,:), '--bo'); set(gca,'XTick',wind_label);
title('Signals424'); xlabel(prop_xlabel); ylabel('deg'); legend({'std:'},'Location','best')
elseif a==3
subplot(425),
% .....
subplot(426),
% .....
elseif a==4
subplot(427),
% ....
subplot(428),
% ....
savefig(name_fig1);
end
if a==5
num2 = open(name_fig2_final);
figure(num2); pause(2); hold on
subplot(6,2,1)
plot(wind_list, signals_stats(17,:), '--bo'); set(gca,'XTick',wind_label); hold on
plot(wind_list, signals_stats(18,:), '--b^'); plot(wind_list, signals_stats(19,:), '--bv');
title('Signals621'); xlabel(prop_xlabel); ylabel('Nm'); legend({'min:','max:', 'mean:'},'Location','best')
subplot(6,2,2),
plot(wind_list, signals_stats(20,:), '--bo'); set(gca,'XTick',wind_label);
title('Signals622'); xlabel(prop_xlabel); ylabel('Nm'); legend({'std:'},'Location','best')
elseif a==6
subplot(6,2,3)
plot(wind_list, signals_stats(21,:), '--bo'); set(gca,'XTick',wind_label); hold on
plot(wind_list, signals_stats(22,:), '--b^'); plot(wind_list, signals_stats(23,:), '--bv');
title('Signals623'); xlabel(prop_xlabel); ylabel('Nm'); legend({'min:','max:', 'mean:'},'Location','best')
subplot(6,2,4),
plot(wind_list, signals_stats(24,:), '--bo'); set(gca,'XTick',wind_label);
title('Signals624'); xlabel(prop_xlabel); ylabel('Nm'); legend({'std:'},'Location','best')
elseif a==7
subplot(6,2,5)
%...
subplot(6,2,6),
%...
elseif a==8
subplot(6,2,7)
%...
subplot(6,2,8),
%...
elseif a==9
subplot(6,2,9)
%...
subplot(6,2,10),
%...
elseif a==10
subplot(6,2,11)
%...
subplot(6,2,12),
%...
savefig(name_fig2);
end
a=a+1;
end
2 个评论
Bjorn Gustavsson
2021-9-7
What I do to avoid these type of problems is to keep the axes-handles of the different subplots and instead of calling subplot again to make one subplot-axes the current I call axes(sph42(1)):
sph42 = subplot(4,2,1);
sph42(2) = subplot(4,2,2);
for i1 = 8:-1:1
sph42(i1) = subplot(4,2,i1)
end
axes(sph42(1))
plot(randn(23))
% etc
If that works with data saved in .fig-format I don't know, but maybe...
HTH
采纳的回答
Dave B
2021-9-7
编辑:Dave B
2021-9-8
TLDR: use tiledlayout/nexttile if you have R2019b or later, on older releases you can work around subplot's save/load weirdness with a 'Tag' or other property and findobj.
subplot's behavior can be awkward with saved files, there's no trivial way to save the axes handles.
If you really want to track these axes across saving, you could consider getting the children of the figure:
subplot(1,2,1)
plot(1:10)
subplot(1,2,2)
plot(rand(1,10))
kids = get(gcf,'Children');
hold(kids(2), 'on')
plot(kids(2), rand(1,10))
As you can see, the tricky bit here is determining which axes is which. You could add a 'Tag' or manipulate another property to determine which one you want, or you could look at the Position property and decode out the location in the grid:
clf
ax=subplot(1,2,1);
plot(1:10)
ax.Tag='1'; % If you set the Tag to '1' on the axes, that information will be saved and gives you a way to find this axes after loading (without calling 'subplot()' which may replace your axes)
ax=subplot(1,2,2);
plot(rand(1,10))
ax.Tag='2';
ax1 = findobj(gcf, 'Tag', '1'); % findobj provides a way to find the axes with the Tag '1'
hold(ax1, 'on')
plot(ax1, ones(1,10))
ax2 = findobj(gcf, 'Tag', '2');
hold(ax2, 'on')
plot(ax2, 1:10)
If you're using R2019b or later though, you're far better off using nexttile. nexttile saves and loads much more smoothly, and you can get a handle to an axes very similarly to how you'd do that with subplot:
clf
tiledlayout(1,2)
nexttile(1); % tiledlayout(1,2) + nexttile(1) is sort of like subplot(1,2,1), but tiledlayout is more modern, works better with saving among other things
plot(1:10);
nexttile(2);
plot(rand(1,10))
% Unlike the subplot equivalent, this should work after saving and loading
ax1 = nexttile(1);
hold(ax1,'on')
plot(ax1, rand(1,10))
ax2 = nexttile(2);
hold(ax2,'on')
plot(ax2, 1:10)
3 个评论
Dave B
2021-9-8
Ah 2013b is a while back. It surprised me at first, as I expected hold to work, but the problem is actually the ax.Tag line, which should be a set( in old releases:
This code worked for me on 2013b:
ax=subplot(1,2,1);
plot(1:10)
set(ax,'Tag','1')
ax=subplot(1,2,2);
plot(rand(1,10))
set(ax,'Tag','2')
hgsave(gcf,'foo.fig')
%%
open('foo.fig')
ax1 = findobj(gcf, 'Tag', '1');
hold(ax1,'on')
plot(ax1, ones(1,10))
ax2 = findobj(gcf, 'Tag', '2');
hold(ax2, 'on')
plot(ax2, 1:10)
更多回答(1 个)
MICHAEL MUTWIRI
2021-9-7
编辑:MICHAEL MUTWIRI
2021-9-7
The subplot has a default of replacing old plot when creating a new plot on the same axis. Add the
subplot(abc, 'NextPlot','add') for each subplot case. You also need to hold on after each subplot and hold off before moving to the next subplot
My code contains sample data to demonstrate a figure with subplots can be saved and opened later for additional plots. I have commented out axis labels since I don't your actual data.
Following your while-loop code, you need to have created the two figures and saved them before opening them inside the while loop
Hope your understand and complete the code for if-elseif conditions for a==5 to a==10
name_fig1_final='name_fig1_final.fig';
name_fig2_final='name_fig2_final.fig';
%% prepare some sample data for testing
name_signals_array =1:24;
wind_list = linspace(0,2*pi,100);
signals_stats = ones(30,length(wind_list));
for i=1:size(signals_stats,1)
signals_stats(i,:)=sin(wind_list+i*0.1*pi);
end
%% name_fig1_final: subplot 4x2
num1 = figure;
subplot(4,2,1);z1 = cos(wind_list);plot(wind_list,z1,'r');grid on
subplot(4,2,2);z2 = cos(wind_list);plot(wind_list,z2,'r');grid on
subplot(4,2,3);z3 = cos(wind_list);plot(wind_list,z3,'r');grid on
subplot(4,2,4);z4 = cos(wind_list);plot(wind_list,z4,'r');grid on
subplot(4,2,5);z5 = cos(wind_list);plot(wind_list,z5,'r');grid on
subplot(4,2,6);z6 = cos(wind_list);plot(wind_list,z6,'r');grid on
subplot(4,2,7);z7 = cos(wind_list);plot(wind_list,z7,'r');grid on
subplot(4,2,8);z8 = cos(wind_list);plot(wind_list,z8,'r');grid on
savefig(num1,name_fig1_final)
close(num1)
%% name_fig2_final: subplot 6x2
num2 = figure;
subplot(6,2,1);zz1 = cos(wind_list);plot(wind_list,zz1,'r');grid on
subplot(6,2,2);zz2 = cos(wind_list);plot(wind_list,zz2,'r');grid on
subplot(6,2,3);zz3 = cos(wind_list);plot(wind_list,zz3,'r');grid on
subplot(6,2,4);zz4 = cos(wind_list);plot(wind_list,zz4,'r');grid on
subplot(6,2,5);zz5 = cos(wind_list);plot(wind_list,zz5,'r');grid on
subplot(6,2,6);zz6 = cos(wind_list);plot(wind_list,zz6,'r');grid on
subplot(6,2,7);zz7 = cos(wind_list);plot(wind_list,zz7,'r');grid on
subplot(6,2,8);zz8 = cos(wind_list);plot(wind_list,zz8,'r');grid on
subplot(6,2,9);zz9 = cos(wind_list);plot(wind_list,zz9,'r');grid on
subplot(6,2,10);zz10 = cos(wind_list);plot(wind_list,zz10,'r');grid on
subplot(6,2,11);zz11 = cos(wind_list);plot(wind_list,zz11,'r');grid on
subplot(6,2,12);zz12 = cos(wind_list);plot(wind_list,zz12,'r');grid on
savefig(num2,name_fig2_final)
close(num2)
%
a=1;
while(a<=length(name_signals_array))
if a==1
% at this stage, the figure need to been created and saved before
% trying to open it.
num1 = open(name_fig1_final);
figure(num1); pause(2);
subplot(421,'NextPlot','add'),
hold on
plot(wind_list, signals_stats(1,:), '--b'); %set(gca,'XTick',wind_label);
plot(wind_list, signals_stats(2,:), '--m'); plot(wind_list, signals_stats(3,:), '--g');
title('Signals421');% xlabel(prop_xlabel); ylabel('rpm'); legend({'min:','max:', 'mean:'},'Location','best')
hold off
subplot(422,'NextPlot','add'),
hold on
plot(wind_list, signals_stats(4,:), '--b'); % set(gca,'XTick',wind_label);
title('Signals422'); %xlabel(prop_xlabel); ylabel('rpm'); legend({'std:'},'Location','best')
hold off
elseif a==2
subplot(423, 'NextPlot','add');
hold on
plot(wind_list, signals_stats(5,:), '--b'); %set(gca,'XTick',wind_label); hold on
plot(wind_list, signals_stats(6,:), '--g'); plot(wind_list, signals_stats(7,:), '--c');
title('Signals423'); %xlabel(prop_xlabel); ylabel('deg'); legend({'min:','max:', 'mean:'},'Location','best')
hold off
subplot(424, 'NextPlot','add'),
hold on
plot(wind_list, signals_stats(8,:), '--b'); %set(gca,'XTick',wind_label);
title('Signals424'); %xlabel(prop_xlabel); ylabel('deg'); legend({'std:'},'Location','best')
hold off
elseif a==3
subplot(425, 'NextPlot','add'),
% .....
subplot(426, 'NextPlot','add'),
% .....
elseif a==4
subplot(427, 'NextPlot','add'),
% ....
subplot(428, 'NextPlot','add'),
% ....
%savefig(name_fig1); take this outside the if-elseif conditions
end
savefig(num1,name_fig1_final)
% Edit the code below in a similar fashion to the avove part
% if a==5
% num2 = open(name_fig2_final);
% figure(num2); pause(2); hold on
% subplot(6,2,1)
% plot(wind_list, signals_stats(17,:), '--bo'); set(gca,'XTick',wind_label); hold on
% plot(wind_list, signals_stats(18,:), '--b^'); plot(wind_list, signals_stats(19,:), '--bv');
% title('Signals621'); xlabel(prop_xlabel); ylabel('Nm'); legend({'min:','max:', 'mean:'},'Location','best')
% subplot(6,2,2),
% plot(wind_list, signals_stats(20,:), '--bo'); set(gca,'XTick',wind_label);
% title('Signals622'); xlabel(prop_xlabel); ylabel('Nm'); legend({'std:'},'Location','best')
% elseif a==6
% subplot(6,2,3)
% plot(wind_list, signals_stats(21,:), '--bo'); set(gca,'XTick',wind_label); hold on
% plot(wind_list, signals_stats(22,:), '--b^'); plot(wind_list, signals_stats(23,:), '--bv');
% title('Signals623'); xlabel(prop_xlabel); ylabel('Nm'); legend({'min:','max:', 'mean:'},'Location','best')
% subplot(6,2,4),
% plot(wind_list, signals_stats(24,:), '--bo'); set(gca,'XTick',wind_label);
% title('Signals624'); xlabel(prop_xlabel); ylabel('Nm'); legend({'std:'},'Location','best')
% elseif a==7
% subplot(6,2,5)
% %...
% subplot(6,2,6),
% %...
% elseif a==8
% subplot(6,2,7)
% %...
% subplot(6,2,8),
% %...
% elseif a==9
% subplot(6,2,9)
% %...
% subplot(6,2,10),
% %...
% elseif a==10
% subplot(6,2,11)
% %...
% subplot(6,2,12),
% %...
% savefig(name_fig2);
% end
%fprintf('a = %g\n',a)
a=a+1;
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!