Plot3 with different colors depending on the value of Z

17 visualizaciones (últimos 30 días)
patr chri
patr chri el 19 de Jun. de 2020
Comentada: patr chri el 19 de Jun. de 2020
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.

Respuesta aceptada

KSSV
KSSV el 19 de Jun. de 2020
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 comentarios
patr chri
patr chri el 19 de Jun. de 2020
Your last suggestion worked.
Thank you!
patr chri
patr chri el 19 de Jun. de 2020
@Image Analyst
I would need to interpolate in between them in order to generate seperate color areas, I think. What would be your suggestion?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by