2016-08-21 102 views
3

我試圖創建一個MATLAB的數字,看起來像這樣: desired figure灌裝用多種顏色(MATLAB,衝浪)曲線上方區域

我這樣做:(一)分配值點到每個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在這些空白處繪製顏色?

謝謝!

回答

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 trinagle 
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 trinagle with white color. 
vert = [0 0;0 1;1 0]; % x and y vertex coordinates 
fac = [1 2 3]; % vertices to connect to make trinagle 
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

謝謝你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

相關問題