How to set a point to change color in colorbar
5 views (last 30 days)
Show older comments
Christos Dimopoulos
on 1 Jan 2018
Edited: Christos Dimopoulos
on 7 Feb 2018
I want to use scatter3 with particles and want to have different color for positive and negative values of charge or velocities. I use this code through a Simulink prototype " Spiral Galaxy Formation Simulation "
oldPlot = scatter3(points_x,points_y,points_z,psize,points_col,'filled')
cmap = [1 0 0 ; 0 1 0 ] ; %2 color bar
colorbar;
colormap(cmap)
but when positive or negative gets higher values, then 0 is not the point that the color changes, but it moves towards the middle of the min and max value.
I want all positive to be green and all negative to be red.
I tried this code
cm=colormap;
cm(0,:)= [1 0 0];
colorbar;
colormap(cm)
but it doesn't seem to work.
2 Comments
Rik
on 1 Jan 2018
Does it need to be one single scatter object? Otherwise you can simply plot the negative part separately from the positive part.
Accepted Answer
Image Analyst
on 1 Jan 2018
Edited: Image Analyst
on 1 Jan 2018
Try this:
% Initialization/clean up
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 13;
numPoints = 100;
points_x = rand(1,numPoints) - 0.5;
points_y = rand(1,numPoints) - 0.5;
points_z = rand(1,numPoints) - 0.5;
psize = 40;
% User wants "all positive to be green and all negative to be red."
% First default to all black;
points_col = zeros(numPoints, 3);
% Now find where ALL 3 coords are positive;
allPosIndexes = points_x > 0 & points_y > 0 & points_z > 0;
% Set those to green.
points_col(allPosIndexes, 2) = 1;
% Now find where ALL 3 coords are negative;
allNegIndexes = points_x < 0 & points_y < 0 & points_z < 0;
% Set those to red.
points_col(allNegIndexes, 1) = 1;
% Now one octant will be red, one octant will be green
% and 6 octants will be black.
% Now plot.
oldPlot = scatter3(points_x,points_y,points_z,psize,points_col,'filled')
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
zlabel('Z', 'FontSize', fontSize);
4 Comments
Image Analyst
on 18 Jan 2018
I've added Simulink to the Product list on the right hand side, so that others don't have to waste their time if they don't (like me) have Simulink. I'd say a minority of people here even have Simulink, so when people have a Simulink program, or any program that requires function available only in a certain toolbox or product they list that in the Products section. You might also attach the .mdl file to your original question up top.
More Answers (1)
See Also
Categories
Find more on General Applications 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!