Plot3 with different colors depending on the value of Z

17 ビュー (過去 30 日間)
patr chri
patr chri 2020 年 6 月 19 日
コメント済み: patr chri 2020 年 6 月 19 日
I am trying to create something like a contour plot but only with vectors. To be more specific, I have 3 vectors of equal lengths and I am using plot3 to visualize their data. Now I am trying to add colors depending on the Z value. I have written the following code, but it doesn't work and I don't know why:
max_iter = find(n_iter(2:end)==0);
MAX = n_iter(max_iter);
MAXVAL = optValues(max_iter);
color = ['b','g','y','r'];
for i = 1:length(MAXVAL)
if(MAXVAL(i) < 1)
plot3(1:1:i,MAX(i),MAXVAL(i),color(1)); hold on;
elseif (MAXVAL(i) < 50)
plot3(1:1:i,MAX(i),MAXVAL(i),color(2));hold on;
elseif (MAXVAL(i) < 400)
plot3(1:1:i,MAX(i),MAXVAL(i),color(3));hold on;
else
plot3(1:1:i,MAX(i),MAXVAL(i),color(4));hold on;
end
end
grid on;
xlabel('Simulation time steps');ylabel('Iteration');zlabel('Objective Function Value');
By "it doesn't work" I mean that MATLAB is busy without producing any results.

採用された回答

KSSV
KSSV 2020 年 6 月 19 日
Let (x,y,z) be your m*3 data. Two options:
If data is structured
x = unique(x) ; nx = length(x) ;
y = unique(y) ; ny = length(y) ;
[X,Y] = meshgrid(x,y) ;
Z = reshape(z,nx,ny) ;
contour(X,Y,Z) ;
If data is unstructured:
m = 100 ; n = 100 ;
xi = linspace(min(x),max(x),m) ;
yi = linspace(min(y),max(y),n) ;
[X,Y] = meshgrid(xi,yi) ;
Z = griddata(x,y,z,X,Y) ;
contour(X,Y,Z)
  6 件のコメント
patr chri
patr chri 2020 年 6 月 19 日
Your last suggestion worked.
Thank you!
patr chri
patr chri 2020 年 6 月 19 日
@Image Analyst
I would need to interpolate in between them in order to generate seperate color areas, I think. What would be your suggestion?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by