7

I want to plot 3D points in Matlab in different colors depending on a value. I've got following code, but this does not work because plot3d needs a vector.

x = vdhf_data.data(:,1);
y = vdhf_data.data(:,2);
z = vdhf_data.data(:,3);
data = vdhf_data.data(:,4);

grid on
hold all

for k=1:length(x)
    if data(k) < 6  
        plot3d(x(k), y(k), z(k), 'ws--', 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'r')
    else
        plot3d(x(k), y(k), z(k), 'ws--', 'MarkerEdgeColor', 'g', 'MarkerFaceColor', 'g')
    end
end

How to do that in Matlab?

1 Answer 1

10

I would use

scatter3(x,y,z,ones(size(x)),data,'filled')

This will plot all the points at the same size and color them according to the value of data, using the current colormap. You can also use data to scale the size of each point.

scatter3(x,y,z,data.^-2,data,'filled')
Sign up to request clarification or add additional context in comments.

3 Comments

I believe instead of ones(size(x)) the size vector in scatter3 can be scalar value for the same size or an empty ([]) for default size.
Yes there can be a scalar. I only want to have two markercolors. Is there a possibility to add this in one line instead of a loop?
You will just need to adjust your colormap. So for two colors (red & blue)you will do something like this colormap([ones(50,1)*[1 0 0]; ones(50,1)*[0 0 1]]) This will make the color division in the middle of the data range. If you want it to be at a specific number you have to scale the ones(n-a,1) ones(n+a,1) accordingly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.