Plot two surfaces each with different color

I want to plot a surface with green color on another surface with red color such that the grids will be transparent. How can I achieve this in Matlab?

 Accepted Answer

I’m not certain what you want.
Try this:
[X,Y] = meshgrid(linspace(-2, 2, 50));
Z = @(x,y,c) x.^2 + y.^2 + c;
figure(1)
surf(X, Y, +Z(X,Y,-4), 'FaceColor','g', 'FaceAlpha',0.5, 'EdgeColor','none')
hold on
surf(X, Y, -Z(X,Y,-4), 'FaceColor','r', 'FaceAlpha',0.5, 'EdgeColor','none')
hold off
grid on

4 Comments

Thank you so much for your answer, Star. I have attached my matlab file, ff.mat.
I use the code below to generate two surface plots. Please, help me: I want the green plot to be centrally placed on the red plot.
load ff
figure(1 )
h(1)= mesh(reshape(FENd.x,M),reshape(FENd.y,M), 'edgecolor','g');
hold on
h(2) = mesh(x,y, 'edgecolor','r');
xlabel('x')
ylabel('y')
zlabel('z')
hold off
grid on
The mesh and similar functions will only plot the first matrix argument (and ignores the second) unless it gets all three (X,Y,Z) coordinate arguments. This code will produce the same as your original code, so you may want to plot ‘Y’ and ‘y’ separately, since your code does not plot them.
In order to centre the green mesh plot on the red one, I introduced two independent variable vectors, ‘xc’ and ‘yc’, and simply offset them by 0.5.
Try this:
X = reshape(FENd.x,M);
Y = reshape(FENd.y,M);
xc = linspace(1, size(X,2), size(X,2));
yc = linspace(1, size(X,1), size(X,1));
figure(1 )
h(1)= mesh(xc+0.5, yc+0.5, X, 'edgecolor','g');
hold on
h(2) = mesh(x, 'edgecolor','r');
xlabel('x')
ylabel('y')
zlabel('z')
hold off
grid on
Experiment to get the result you want.
Thank you so much, Star. This is exactly what I want.
My pleasure.
If my Answer helped you solve your problem, please Accept it!

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!