scatter3, stop the color changing after a second scatter3 command
Show older comments
I am having some problems with scatter3, this is an example code:
figure
colormap jet
a = scatter3([1 2 3], [1 2 3],[1 2 3], 50, [1; 2; 2], 'filled')
hold on
scatter3([0 1 2], [1 2 3],[1 2 3], 50, [1; 2; 3], 'filled')
hold off
After the second scatter3 command the colors of the first scatter change accordingly. I want to remove this effect. How can I do so?
Thank you!
4 Comments
Lorenzo Chiaverini
on 10 Apr 2021
Edited: Lorenzo Chiaverini
on 10 Apr 2021
Walter Roberson
on 10 Apr 2021
colormap(parula);
scatter3(comp1, comp2, comp3, 10, normvec, 'filled')
caxis([0 maximum_norm_value_to_map]);
where maximum_norm_value_to_map would be the value beyond which you did not want to change colors with increasing norm. The idea is that maximum_norm_value_to_map would be some absolute maximum, and then colors would be mapped on an absolute scale, so that if you looked at two different plots with two different ranges of norms, then the same color would correspond to the same absolute norm value (not to the same relative proportion of the local maximum for that plot.)
Lorenzo Chiaverini
on 10 Apr 2021
Lorenzo Chiaverini
on 10 Apr 2021
Edited: Lorenzo Chiaverini
on 10 Apr 2021
Accepted Answer
More Answers (1)
This might be a bit tricky to explain what is happening. By specifying C as a vector, the following method is used to determine the color.
- If C is a vector with length equal to the length of X, Y, and Z, then the values in C are linearly mapped to the colors in the current colormap.
What the actual color is depends on what the current colormapping is. Dy default, the colormap is applied to the range of your color data. When you plot the first scatter3 plot, your color values range from 1 to 2. You can see this if you turn on the colorbar.
scatter3([1 2 3], [1 2 3],[1 2 3], 50, [1; 2; 2], 'filled')
colormap jet
colorbar
What happens is that, when you add your second plot, the range of your color data changes to 1-3. The colormap is rescaled to include the new data, now making 3 dark red, and moving 2 into the center of the range, changing all points with C==2 to green.
figure
scatter3([1 2 3], [1 2 3],[1 2 3], 50, [1; 2; 2], 'filled')
hold on
scatter3([0 1 2], [1 2 3],[1 2 3], 50, [1; 2; 3], 'filled')
hold off
colormap jet
colorbar
There are two ways I can think of to fix this.
- Use caxis to set the color range up front
- Specify rgb values for your colors rather than the vector approach you are using.
% Method 1 - caxis
figure
scatter3([1 2 3], [1 2 3],[1 2 3], 50, [1; 2; 2], 'filled')
colormap jet
colorbar
caxis([1 3])
% Method 2 - RGB
figure
cmap=[0 0 1;0 1 0;1 0 0];
scatter3([1 2 3], [1 2 3],[1 2 3], 50, cmap([1;2;2],:), 'filled')
hold on
scatter3([0 1 2], [1 2 3],[1 2 3], 50, cmap([1;2;3],:), 'filled')
hold off
colormap jet
Categories
Find more on Orange in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


