30 views (last 30 days)

Show older comments

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.

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);

Image Analyst
on 18 Jan 2018

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!