MATLAB Answers

Plot3 with different colors depending on the value of Z

27 views (last 30 days)
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;
plot3(1:1:i,MAX(i),MAXVAL(i),color(4));hold on;
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.

Accepted Answer

KSSV on 19 Jun 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) ;
Christos Patrinos
Christos Patrinos on 19 Jun 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?

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!