How to set a point to change color in colorbar
4 次查看(过去 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 中查找有关 General Applications 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!