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

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?

Answers (1)

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 Comments

Really appreciate your reply and have tried using this and works perfectly for this interval. This may be a stupid issue but when I tried to change the interval increment to 10 instead of 20 (for when I use 10-second data) using the following code (adding to yours but probably did something wrong) I got the same problem of not aligning the two graphs:
n=20
x = (0:n:340)+(n/2);
bar(x, rand(1, length(x)), 'BarWidth', 1, 'FaceColor', 'b')
hold on
bar(x, -rand(1, length(x)), 'BarWidth', 1, 'FaceColor', 'r')
plot(rand(1, (length(x)*20))-0.5, 'Color', 'g', 'LineWidth', 2 )
(so for n=20 - it is your code and works perfect. but for n=10 I get the same issue)
Nevertheless many thanks!
Silly mistake, I found it!! Many thanks for your help much appreciated.
You should use
x = (0:n:360-n)+(n/2);
and not hard-code "*20" in the plot command ;-) but of course "*n".

Sign in to comment.

Asked:

on 18 Apr 2017

Commented:

on 18 Apr 2017

Community Treasure Hunt

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

Start Hunting!