MATLAB: Colorbar is not representing the current colormap correctly

colorbar colormap scatter3 plot jet

Hi There! I made myself a semy custom colormap based on jet with a custom length. I then assign other values I have to each of the colors. In a plot, I plot points colored based on this assignment. When I then use colorbar, it does not contain all of the colors that should be in the colormap. The values that colors get assigned to are negative and positive.
In my sample, I clearly get some bright red spots that are not represented by the colorbar.
here is some sample code:
% sample code
test = rand(2000,3);
colormap = jet(2000);
scatter3(test(1:end,1), test(1:end,2), test(1:end,3),30,colormap,'filled');
colorbar;

Best Answer

You have given true RGB values to scatter3 from mapping your data via the colourmap so the colourmap of the axes is still Parula, which is what the colourbar shows.
When you give colours in true RGB they are unaffected by the colourmap of the axes and giving an instruction to the scatter3 command does not affect the axes colourmap at all.
colormap( jet(2000) )
would set the colourmap of the axes to be the same as your data, but then there is no reason to give true RGB colours to scatter3.
figure; scatter3(test(1:end,1), test(1:end,2), test(1:end,3), 30, 1:2000, 'filled' );
colormap( jet(2000) )
would achieve the same thing. This would also mean that your data is actually affected by the colourmap so if you change it to another one or use caxis to compress it your data will change accordingly. If you don't want this to happen then your original code is fine, but you don't want to put a colorbar up if your data is not using it.
Also don't name a variable colormap - it is the name of the function that sets the colourmap of the axes which you can no longer use because you have hidden it behind a variable name.