How to set a point to change color in colorbar
조회 수: 5 (최근 30일)
이전 댓글 표시
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
Rik
2018년 1월 1일
Does it need to be one single scatter object? Otherwise you can simply plot the negative part separately from the positive part.
채택된 답변
Image Analyst
2018년 1월 1일
편집: Image Analyst
2018년 1월 1일
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
Image Analyst
2018년 1월 18일
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.
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!