title: Matlab: Stacking of various plots
body: <p>I have a question regarding the following graph:</p>
  
  <p><img src="https://i.stack.imgur.com/TWbNP.jpg" alt="enter image description here">
  Essentially, we have here two subplots that have the same x-axis. I would like
  to save space now, and instead of having 2 subplots with two x-axix labels, I would
  like to eleminiate the space between them and stack them on top of each other.</p>
  
  <p>Is there any easy way to do that?</p>
  
  <p>Many thanks!
  Patrick</p>
  
  <pre><code>%% Data
  t  = 0:100;
  f1 = 0.3;
  f2 = 0.07;
  u1 = sin(f1*t);   cu1 = 'r'; %red
  u2 = cos(f2*t);   cu2 = 'b'; %blue
  v1 = 5*u1.^2;     cv1 = 'm'; %magenta
  v2 = 5*u2.^2;     cv2 = 'c'; %cyan
  
  figure;
  h(1) = subplot(2,1,1); % upper plot
  plot(t,u1,'Color',cu1,'DisplayName','u1'); hold on;
  plot(t,u2,'Color',cu2,'DisplayName','u2'); hold off;
  
  xlabel('Time t [s]');
  ylabel('u [some unit]');
  legend(gca,'show');
  
  h(2) = subplot(2,1,2); % lower plot
  plot(t,v1,'Color',cv1,'DisplayName','v1'); hold on;
  plot(t,v2,'Color',cv2,'DisplayName','v2'); hold off;
  
  xlabel('Time t [s]');
  ylabel('v [some unit^2]');
  legend('show');
  
  linkaxes(h,'x'); % link the axes in x direction (just for convenience)
  </code></pre>