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

时间:2016-08-21 16:21:08

标签: matlab colors 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在这些空格中绘制颜色?

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以尝试使用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

答案 1 :(得分:1)

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

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

相关问题