How to add a color-dependent legend to scatter3 plot?
Show older comments
I have a 3D histogram via scatter3, but I wish to add a legend to state the frequency signified by the color. How do I do this?
Also, I would appreciate any simplifications you could suggest to my code below.

%%Plot a scatter3 histogram of the shifts.
xdata = tumorshifts(:,1); ydata = tumorshifts(:,2);
zdata = tumorshifts(:,3);
size = 10*shiftno;
for loop = 1:length(size)
if isequal(shiftno(loop),1)
size(loop) = size(loop)/10;
end
end
color = zeros(length(shiftno),3);
values = unique(shiftno);
% Resource: http://www.wolframalpha.com/input/?i=orange+RGB
% http://www.wolframalpha.com/input/?i=576+nm+light+to+RGB
for loop = 1:length(color)
if shiftno(loop) == values(1)
color(loop,1) = 130; color(loop,2) = 130; color(loop,3) = 130; % grey
elseif shiftno(loop) == values(2)
color(loop,1) = 41; color(loop,2) = 0; color(loop,3) = 255; % 452 nm
elseif shiftno(loop) == values(3)
color(loop,1) = 0; color(loop,2) = 214; color(loop,3) = 69; % 514 nm
elseif shiftno(loop) == values(4)
color(loop,1) = 132; color(loop,2) = 252; color(loop,3) = 0; % 576 nm
elseif shiftno(loop) == values(5)
color(loop,1) = 255; color(loop,2) = 8; color(loop,3) = 0; % 638 nm
elseif shiftno(loop) == values(6)
color(loop,1) = 158; color(loop,2) = 0; color(loop,3) = 0; % crimson
end
end
scatter3(xdata,ydata,zdata,size,color/255)
axis equal
title(['Histogram of ',num2str(length(unique(elektapatients.StudyID))),...
' Elekta Patients'' Daily Shifts'])
xlabel('Left [mm]')
ylabel('Superior [mm]')
zlabel('Posterior [mm]')
Accepted Answer
More Answers (0)
Categories
Find more on Legend in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!