Matching x-axis when two plots's axes are multiple of each other

조회 수: 3 (최근 30일)
Stefan Cutajar
Stefan Cutajar 2017년 4월 18일
댓글: Thorsten 2017년 4월 18일
I have the following 2 subplots. One Bar graph and one normal line graph as shown in the figure.
They are both using same data but bargraph is using 20-second data and the line graph is using 1-second data so x-axes are multiples of each other (Bar graph 1:18 and Line graph 1:18*20 (1:360)). How can I match these two plots. I want them to be exactly underneath each other (ideally in the same plot but was not able to do it in same plot and have now tried to do them on seperate plots). I have the following code right now:
ax2=subplot(4,1,2);
bar(BVol,'b','BarWidth',1);
set(gca,'XLim',[0 numel(BVol)+1]);
hold on
bar(-SVol,'r','BarWidth',1);
hold on
%plot(VRatio,'w','LineWidth',2);
%plot(VRatio.*((max(abs([BVol; SVol])))./(max(abs(VRatio)))),'w','LineWidth',2);
legend('BVol', 'SVol', 'VRatio','Location','northwest','Orientation','horizontal');
set(get(gca,'ylabel'),'Rotation',0,'VerticalAlignment','middle');
set(get(gca,'ylabel'), 'Units', 'Normalized', 'Position', [-0.1, 0.5, 0]);
set(gca,'Color',[0 0 0]);
%set(gca,'Position',[.05 .365 .9 .32]);
set(gca,'xtick',[]);
%
ax3=subplot(4,1,3);
bar(TBR,'FaceColor',[0.8627 0.6510 0],'BarWidth',1); %Orange
hold on
bar(-TAR,'FaceColor',[0 0.5882 0],'BarWidth',1); %Green
hold on;
%plot(SN2(:,1),SN2(:,2),'Color',[1 0.651 0],'LineWidth',2);
%plot(NBR.*((max(abs([TBR; TAR])))./(max(abs(NBR)))),'Color',[1 0.651 0],'LineWidth',2);
hold on
%plot(NAR.*((max(abs([TBR; TAR])))./(max(abs(NAR)))),'Color',[0 0.8824 0],'LineWidth',2); %Lighter green
legend('TBR', 'TAR', 'NBR','NAR','Location','northwest','Orientation','horizontal');
set(get(gca,'ylabel'),'Rotation',0,'VerticalAlignment','middle')
set(get(gca,'ylabel'), 'Units', 'Normalized', 'Position', [-0.1, 0.5, 0]);
set(gca,'Color',[0 0 0]);
set(gcf,'Units','normal');
%set(gca,'Position',[.05 .03 .9 .32]);
(sorry for the lack of neatness)
Is there a way to do this please?

답변 (1개)

Thorsten
Thorsten 2017년 4월 18일
편집: Thorsten 2017년 4월 18일
With the x value of the bar command you can plot the two graphs on top of each other, as you initially intended:
x = (0:20:340)+10;
bar(x, rand(1, 18), 'BarWidth', 1, 'FaceColor', [0.8627 0.6510 0])
hold on
bar(x, -rand(1, 18), 'BarWidth', 1, 'FaceColor', [0 0.5882 0])
plot(rand(1, 360)-0.5, 'Color', [1, 0.651, 0], 'LineWidth', 2 )
You can also use the bar and the plot command in different subplots and have them aligned.
  댓글 수: 3
Stefan Cutajar
Stefan Cutajar 2017년 4월 18일
Silly mistake, I found it!! Many thanks for your help much appreciated.
Thorsten
Thorsten 2017년 4월 18일
You should use
x = (0:n:360-n)+(n/2);
and not hard-code "*20" in the plot command ;-) but of course "*n".

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by