How to stack layers of 2D plot in horizontally instead of vertically?

7 views (last 30 days)
I have simple code which stacks 3 layers vertically but I am looking for a way that I can stack them horizontally or in other words along x/y axis and the code looks like as flows:
clear all;clc
x = -2:.1:2;
y = x;
z = rand(length(x),length(y));
for k = 1:15:length(z)
z1 = ones(size(z))*k+2;
surf(x,y,z1,abs(z))
hold on
end
xlim([-2.5 2.5])
ylim([-2.5 2.5])
zlim([-5 50])
set(gca,'Ydir','Normal');
colormap ('jet');
shading('flat');

Answers (1)

Walter Roberson
Walter Roberson on 8 Jun 2015
number_of_layers = 3;
relative_spacing = 0.1; %10%
x = -2:.1:2;
y = x;
z = rand(length(x),length(y),number_of_layers);
y_range = max(y)-min(y);
per_layer_offset = y_range * (1 + relative_spacing);
z1 = ones(size(z,1), size(z,2));
for k = 1:number_of_layers
this_y = y + (k-1) * per_layer_offset;
surf(x, this_y, z1, z(:,:,k));
hold on
end
xlim([-2.5 2.5]);
ylim([-2.5 max(this_y)]); %you will need to play with this
zlim([-5 5]); %whatever
  2 Comments
ali nassib
ali nassib on 8 Jun 2015
Thanks for your answers. What I am looking for is something looks like the figure I have shown below.
Many thanks
Walter Roberson
Walter Roberson on 8 Jun 2015
number_of_layers = 3;
X_spacing = 1.234; %adjust per tastes, 1 is valid
first_X = 0.5; %adjust per tastes, 1 is valid
x = -2:.1:2;
y = x;
nx = length(x);
ny = length(y);
z = rand(nx,ny,number_of_layers);
[Ycoord, Zcoord] = ndgrid(x, y);
for K = 1 : number_of_layers
Xcoord = (first_X + X_spacing * (K-1)) * ones(nx, ny);
surf(Xcoord, Ycoord, Zcoord, z(:,:,K));
hold on
end
ylim([-2.5 2.5]);
zlim([-2.5 2.5]);
and you will want to adjust the view.
You should also be considering creating a hgtransform as that would allow you to create your panes like you were originally, but then rotate them without rotating the axes.
You should also be thinking about using slice()

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!