How to align Xaxes for a 2x1 subplot with legends outside and one plot has 2 y-axes
3 次查看(过去 30 天)
显示 更早的评论
Here is some sample code:
figure;
%set x limits
xls = [1, 20];
%%SubPlot 1
%top plot
subplot(2,1,1)
%put some stuff on the left axis
yyaxis left
hold on
apf = plot(1:20,rand(1,20),'r--','linewidth',2);
azp = plot(1:50,rand(1,50),'b:','linewidth',2);
hold off
%set font size, make text bold, set background color to white
fontpt = 14;
set(gca,'FontSize',fontpt,'fontWeight','bold',...
'XColor',[0 0 0],'YColor',[0 0 0])
set(findall(gcf,'type','text'),'FontSize',fontpt,'fontWeight','bold')
set(gcf,'color','w')
ylabel('\color{red}Thing1 \color{black}and \color{blue}Thing2','Interpreter','TeX')
%put some stuff on the right axis
yyaxis right
ayp = plot(1:30,rand(1,30),'m-.','linewidth',2);
%set legend, axis labels, and title
lega=legend([apf,azp,ayp],{'Thing1 is great','Thing2 is better', 'Thing3 is best'},'location','northeastoutside');
xlabel('X Axis')
ylabel('\color{magenta}Thing 3','Interpreter','TeX')
title('Things')
%set x limits
xlim(xls)
%set font size, make text bold, set background color to white
fontpt = 14;
set(gca,'FontSize',fontpt,'fontWeight','bold',...
'XColor',[0 0 0],'YColor',[0 0 0])
set(findall(gcf,'type','text'),'FontSize',fontpt,'fontWeight','bold')
set(gcf,'color','w')
%%Subplot 2
%bottom plot
subplot(2,1,2)
%put some stuff on the plot
hold on
ppf = plot(1:32,rand(1,32)*180,'r--','linewidth',2);
pzp = plot(1:40,rand(1,40)*(-180),'b:','linewidth',2);
pyp = plot(1:20,rand(1,20)*180,'m-.','linewidth',2);
hold off
%set legend, axis labels, and title
legp=legend([ppf,pzp,pyp],{'Thing4','Thing5', 'Thing6'},'location','northeastoutside');
xlabel('X Axis')
ylabel('More Things')
title('More Things')
%set x limits
xlim(xls)
%set font size, make text bold, set background color to white
fontpt = 14;
set(gca,'FontSize',fontpt,'fontWeight','bold',...
'XColor',[0 0 0],'YColor',[0 0 0])
set(findall(gcf,'type','text'),'FontSize',fontpt,'fontWeight','bold')
set(gcf,'color','w')
I would like the x axes to align between the two plots and hold for figure resizing. I have tried using the first plot's position as well as pbaspect to get the plots to align, but to no avail. I have found that the position and and plot box aspect ratio sizes seem to contain the legends, which differ between the two plots and throw off my ability to determine a solution. I can mess with the ratios to get them to look good enough, but then it all gets thrown off when the window is resized. With the legend on the outside, the x axes don't align even if the legends are the same size due to the second yaxis, but they do align if the legends are on the inside and any size. Any help would be much appreciated.
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!