x = 0:0.1:10;
c = get (0, 'defaultaxescolororder');
for n = 1:2
  hax1(n) = subplot (2,1,n);
  h1(n) = plot (x, sin(n*x), 'color', c(n,:));
  hax2(n) = axes ('position', get (hax1(n), 'position'));
  h2(n) = plot (x, cos(n*x), 'color', c(n,:), 'linestyle', '--');
end
set (hax2, 'color', 'none')
legend (hax1(1), {'sin(x)', 'cos(x)'})

legend (hax1(2), [h1(n),h2(n)], {'sin(x)', 'cos(x)'})
