problem with hod on in subplots with same axes
1 view (last 30 days)
Show older comments
Hi, I have two subplots with different series. After creating the two subplots I change the axis position such that they have the same x-Axes. After I need to add some other variable to these subplots keeping the old series. I have set hold on for all the series and this works well if I don't unify the x-axis. The code I used is below:
if true
subplot(2,1,1);
barChartPlot2(Prices(1:end,2),Prices(1:end,3),Prices(1:end,4),Prices(1:end,5));
set(gca,'XLim',[0 rows(H)]);
hold on
hold all
h2=plot(t',min1(1:end),'r','LineWidth',2);
hold on
hold all
h3=plot(t',max1(1:end),'b','LineWidth',2);
hold on
hold all
t=1:rows(ATRD);
subplot(2,1,2);
h4=plot(t',ATRD(1:end),'k','LineWidth',2);
hold on
hold all
h4b=plot(t',ATRDM(1:end),'r','LineWidth',2);
hold on
hold all
h5=plot(t',zeros(1:rows(H),1),'k','LineWidth',2);
hold on
hold all
%%%Same axis
ax=get(gcf,'children');
pos=cell2mat(get(ax,'pos'));
for ii=length(ax):-1:1
if (~strcmpi(get(ax(ii),'type'),'axes'))|(strcmpi(get(ax(ii),'tag'),'legend'))
ax(ii)=[];
end
end
ytop=pos(:,2)+pos(:,4);
[ytop,idx]=sortrows(-ytop);
ax=ax(idx);
pos=pos(idx,:);
HH=(pos(1,2)+pos(1,4))-pos(end,2); %total height of axes
Hused=sum(pos(:,4));
stretch=max(HH/Hused,1);
pos(1,2)=pos(1,2)-pos(1,4)*(stretch-1);
pos(1,4)=pos(1,4)*stretch;
pos(2,4)=pos(2,4)*stretch;
set(ax(1),'pos',pos(1,:));%OKOK
for ii=2:length(ax)
pos(ii,2)=pos(ii-1,2)-pos(ii,4);
set(ax(ii),'pos',pos(ii,:));
end
set(ax,'Yaxislocation','left');
set(ax(end),'xaxislocation','bottom');
xlims=get(ax,'xlim');
xlim=[min([xlims{:}]) max([xlims{:}])];
set(ax,'xlim',xlim);
set(ax(1,:),'Xtick',1:21:rows(H))
set(ax(2,:),'Xtick',1:21:rows(H),'XTickLabel',datestr(Data(1:7:end),'dd-mm HH:MM'));
linkaxes(ax,'x');
end
Basically if I run the code until the line you see OKOK, if later I recall subplot(2,1,1) to write the other series the old ones are kept. While if I run to the next line and than I recall subplot(2,1,1)all the series are erased and the subplot turns white.
I have no Idea about it.
I hope someone can help me.
Gaia
0 Comments
Answers (0)
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!