필터 지우기
필터 지우기

Plot3 with different colors depending on the value of Z

조회 수: 19 (최근 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 CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by