gpt4 book ai didi

matlab - 用多种颜色填充曲线上方的区域(matlab,surf)

转载 作者:太空宇宙 更新时间:2023-11-03 20:33:21 25 4
gpt4 key购买 nike

我正在尝试在 matlab 中创建一个如下所示的图形: desired figure

我这样做是通过:(i) 为每个 x,y 坐标分配值点,(ii) 绘制冲浪图,以及 (iii) 更改视点,以便看不到第三个轴。这是代码:

    x = linspace(0, 1, 10);
y = linspace(0, 1, 10);
z = linspace(0, 1, 10);
z = repmat(z, 10, 1);
z = flipud(triu(z));
z(z==0) = nan;

hold off
surf(x, y, z, 'linestyle', 'none')
colormap([linspace(0.39, 1, 20)',linspace(0.58, 0.25, 20)', linspace(0.93, 0.25, 20)']);
colorbar
xlim([x(1) x(end)])
shading interp
view([90 -90])
hold on
plot(x, 1-y, 'linewidth', 2)

我得到下图:matlab figure I get

如您所见,线条上方有很多空白区域,我也希望它们是彩色的。不幸的是,我无法再添加任何网格点,因为计算这些点的实际值需要花费大量时间(与上面的示例不同)。

有没有办法让 matlab 在那些空白处也绘制颜色?

谢谢!

最佳答案

您可以尝试使用patch函数来创建填充多边形。
参见 http://www.mathworks.com/help/matlab/ref/patch.html

试试下面的代码:

vert = [0 1;1 1;1 0]; % x and y vertex coordinates
fac = [1 2 3]; % vertices to connect to make triangle
fvc = [1 0 0; 1 1 1; 0 0 1];
patch('Faces',fac,'Vertices',vert,'FaceVertexCData',fvc,'FaceColor','interp');

结果很接近:
enter image description here


我设法接近了想要的数字:

close all

x = linspace(0, 1, 10);
y = linspace(0, 1, 10);

%colorbar
xlim([x(1) x(end)])

%Fill rectangle.
vert = [0 0; 1 0; 1 1; 0 1]; % x and y vertex coordinates
fac = [1 2 3 4]; % vertices to connect to make squares
%patch('Faces',fac,'Vertices',vert,'FaceColor','red')
fvc = [1 0 0; 0.6 0.7 1; 0.6 0.7 1; 1 0 0]; %Color of vertices (selected to be close to example image).
patch('Faces',fac,'Vertices',vert,'FaceVertexCData',fvc,'FaceColor','interp')
hold on

%Fill lower triangle with white color.
vert = [0 0;0 1;1 0]; % x and y vertex coordinates
fac = [1 2 3]; % vertices to connect to make triangle
fvc = [1 1 1; 1, 1, 1; 1, 1, 1]; %White color
patch('Faces',fac,'Vertices',vert,'FaceVertexCData',fvc,'FaceColor','interp');

plot(x, 1-y, 'linewidth', 2)

set(gca,'Xtick',[],'Ytick',[]); %Remove tick marks

结果:
enter image description here

关于matlab - 用多种颜色填充曲线上方的区域(matlab,surf),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39066326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com