Plot3 with different colors depending on the value of Z

15 次查看(过去 30 天)
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
@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 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by