When you add the original data in range 1 to 2, the min and max are used to set the caxis. The minimum value (1) is mapped to the first entry in the color map, and the maximum value (2) is mapped to the last entry in the colormap.
When you add the additional data with color info, the min and max of all the data in the axis is used, so the caxis expands to [1 3] . The min value (1) maps to the first entry, the max value (3) maps to the last entry, and the 2 values interpolate over [1 3] and so map to the middle of the color map.
When you say that you do not want the colors of the first scatter to change, that could be interpreted as saying that you want the 1's to map to the first color, and you want the 2's to continue to map to the last color, and you want the 3's to map to.... it isn't clear what you would want them to map to?
If you are doing some kind of presentation over time where you want to show the first version and let the user see it, and then show the second version, and what you are concerned about is consistency over time rather than about the exact colors, then there are multiple approaches:
- Before doing the first scatter3(), do a caxis() call that gives the minimum and maximum of all of the data you are going to plot. That will set axes CLimMode to 'manual' and so the 1 and 2 of the original scatter3 will already be color interpolated over the entire range, so the mapping will be consistent over time; Or
- switch to supplying RGB colors for each color instead of a vector of color values. rescale() and ind2rgb() can help with that.