Matlab: Stacking Various Charts

I have a question regarding the following graph:

enter image description here Essentially, we have two subheadings that have the same x axis. I would like to save some space now, and instead of having 2 subtitles with two x-axix labels, I would like to erase the space between them and stack them on top of each other.

Is there an easy way to do this?

Many thanks! Patrick

%% 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)
+5
source share
1 answer

Remove the arrows for the upper section:

set(h(1),'xticklabel',[]);

and also remove xlabel (delete or comment on this line in your code)

Now move the two closer to each other by changing their position:

pos=get(h,'position');
bottom=pos{2}(2);
top=pos{1}(2)+pos{1}(4);
plotspace=top-bottom;
pos{2}(4)=plotspace/2;
pos{1}(4)=plotspace/2;
pos{1}(2)=bottom+plotspace/2;

set(h(1),'position',pos{1});
set(h(2),'position',pos{2});

then you get

enter image description here

The Y axis labels begin to overlap, so you can also customize them with

set(h(1),'ytick',[-0.5 0 0.5]);

for example, or place one y axis on the right side with:

set(h(2),'YAxisLocation','right')
+11
source

All Articles