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

Filling an area above a curve with many colors (matlab, surf)

我正在尝试在 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');

结果接近:


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

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

结果:

谢谢罗腾!我不知道补丁功能,确实它解决了这个问题! 我试图实现的实际图形上的颜色不是线性的,所以我只是对所有空三角形使用了补丁。这是我在简单示例中使用的调整后的代码(同样,这只是为了能够在曲线上方的区域中使用非线性颜色而更通用):

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

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

patch_cor_y = kron((length(y):-1:1)', ones(3, 1));
patch_cor_x = kron((1:length(x))', ones(3, 1));
patch_cor = [y(patch_cor_y(2:end-2))', x(patch_cor_x(3:end-1))'];
patch_path = reshape(1:length(patch_cor),3,  length(patch_cor)/3)';

patch_col = z(sub2ind(size(z), patch_cor_x(3:end-1), patch_cor_y(2:end-2)));

patch('Faces',patch_path,'Vertices',patch_cor,'FaceVertexCData',patch_col,'FaceColor','interp', 'EdgeColor', 'none');

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

达到的数字:figure