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?

 採用された回答

Star Strider
Star Strider 2018 年 4 月 4 日

5 投票

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 件のコメント

Samuel
Samuel 2018 年 4 月 4 日
編集済み: Samuel 2018 年 4 月 4 日
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
Star Strider
Star Strider 2018 年 4 月 4 日
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.
Samuel
Samuel 2018 年 4 月 4 日
Thank you so much, Star. This is exactly what I want.
Star Strider
Star Strider 2018 年 4 月 4 日
My pleasure.
If my Answer helped you solve your problem, please Accept it!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by