MATLAB: 3D scatter plot with different color and legend explaining each color.

3d plotcolored plotscatter plot

Hi,I want to draw a 3D scatter plot with different colors and want to put a legend that explains what each color means.
My example is something like this.
b's are the data points that I want to put in the 3D scatter. b1=[1,2,3,4,5] b2=[2,2,3,3,1] b3=[1,5,4,4,3]
s is the size of the points s=[1,1,1,1,1]
r is the color of the points (three colors) r=[0,0,2,1,2]
Finally the scatter plot is this = scatter3(b1,b2,b3,s,r,'filled')
My questions are. 1)Can I set a designate color to each numbers in r? for example, red for 0, blue for 1, green for 2. if so, How can I do it?
2) how can I set a legend that explains what the color means? for example. for color 0 – 'efficient', for color 1 – 'inefficient', for color 2- 'not an eq'

Best Answer

Try this:
b1=[1,2,3,4,5];
b2=[2,2,3,3,1];
b3=[1,5,4,4,3];
s=[1,1,1,1,1];
r=[0,0,2,1,2];
scatter3(b1,b2,b3,s*50,r,'filled')
% Define rgb colors manually:
colors = [1 0 0; % red
0 0 1; % blue
0 1 0];% green
colormap(colors);
cb = colorbar;
caxis([-0.5 2.5])
set(cb,'ytick',0:2,'yticklabel',{'efficient','inefficient','not an eq'})
I had to increase the size of the points (multiplied by 50) because otherwise they're too small to see.