Matching x-axis when two plots's axes are multiple of each other
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!