How can I arrange several plots in one figure in 3D space?

My problem is as follows: I have got data from a finite element solver (FlexPDE) that specify the value of a certain variable A in 2D planes (x,y) at different heights z. I can create 2D pcolor and contour plots from these data that show the value of A at given heights, in this case z1,z2 and z3. Now I want to include all these three plots in 3D space in one single figure, where each plot is shown at the height for which its data were calculated (i.e. z=z1,z2,z3 depending on plot). What is the best way to do this?
Essentially, I'm looking for results similar to slice() plots, but in my case I don't have a 3D array of data, but rather several 2D arrays of data. I've tried using slice() by combining the arrays, but I can't edit each 2D plot separately, I think (the colormap is my main issue).

 Accepted Answer

Consider the following example:
[x,y,c] = peaks;
g1 = hgtransform('Matrix',makehgtform('translate',[0 0 -0.5]))
h1 = surface(x,y,zeros(size(z)),z,'Parent',g1)
g2 = hgtransform('Matrix',makehgtform('translate',[0 0 0.5]))
[~,h2] = contour(x,y,z,'Parent',g2)
zlim([-1 1])
view(3)
The hgtransform objects allow you to move their children around by using their Matrix property. I've moved the first hgtransform in - Z, and the second hgtransform in + Z.
The call to surface with zeros as the Z creates the same object that the pcolor command would have, but lets me parent it to the hgtransform object. The contour command is happy using the hgtransform as a parent directly.
You need to tell it you want a 3D view in this case, because none of the plotting commands did that for you.
The result looks like this:
You should be able to modify this to get the effect you want with your data.

3 Comments

Thanks a lot for your answer, I was able to modify it and now it works. However, I've run into another problem: Values of my variable at high z (vertical axis) are significantly smaller than at low z. The same colormap is used for all three plots, which means that variation in pcolor plots at high z is almost invisible. Is there a way to change the colormaps for the three plots separately? In particular, I would like a different range of color values for each plot. I've tried the freezeColors function from the File Exchange, but without success.
Not really. All of the surfaces are going to share the same colormap. If you create a really large colormap, it might be possible to get the freezeColors approach to work, but ...
You might be a little more successful using ind2rgb . This will let you convert the data values into RGB colors.
[x,y,z] = peaks;
c1 = randn(size(z));
ncolors1 = 64;
cmap1 = cool(ncolors1);
c2 = rand(size(z));
ncolors2 = 64;
cmap2 = hot(ncolors2);
i1 = floor(1+(ncolors1-1)*(c1-min(c1(:)))/(max(c1(:))-min(c1(:))));
rgb1 = ind2rgb(i1,cmap1);
surf(x,y,z,rgb1,'FaceColor','flat');
i2 = floor(1+(ncolors2-1)*(c2-min(c2(:)))/(max(c2(:))-min(c2(:))));
rgb2 = ind2rgb(i2,cmap2);
hold on
surf(x+3,y+3,z,rgb2,'FaceColor','flat');
You can easily combine multiple surfaces with RGB colors in a single axes. You can also mix surfaces with RGB colors and surfaces with colormapped colors in the same axes. However, colorbar is only going to know what's going on for the colormapped ones.
This is a great answer, thank you very much. The ind2rgb function did exactly what I wanted. I guess there's no way to manually create a colorbar showing which value of the variable maps to which RGB value? I'm not too worried about that in this case, it would just be interesting to know whether it's possible in general.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!