![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203404/0000%20Screenshot.png)
How to crate a two direction colorbar?
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi,
I am using scatter to generate a 2-D plot. My y-axis values range between, let's say, ylim([-100 100]). The direction of the colorbar, in the jet color scale, will be "dark blue" for -100 up to "dark red" for 100. Is there any way I can have a double direction colorbar centered around zero? For instance, 0 will be the "dark blue", while -100 and 100 will be the "dark red". Of course the colors inbetween will correspond to the values between [0 100] and [-100 0].
댓글 수: 0
채택된 답변
Image Analyst
2019년 2월 9일
Sure. Try this:
numPoints = 500;
y = -100 + 200 * rand(1, numPoints);
x = 10 * rand(1, numPoints);
% Assign colors
cm = [flipud(jet(128)); jet(128)];
% Make 200 long.
cm = imresize(cm, [200, 3]);
markerColors = zeros(length(y), 3);
for k = 1 : length(y)
thisValue = round(y(k) + 100);
if thisValue < 1
thisValue = 1;
elseif thisValue > size(cm, 1)
thisValue = size(cm, 1);
end
markerColors(k, :) = cm(thisValue, :);
end
scatter(x, y, 35, markerColors, 'filled');
% Put a line at the x axis
grid on;
line(xlim, [0, 0], 'Color', 'k', 'LineWidth', 2);
![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203404/0000%20Screenshot.png)
댓글 수: 3
Image Analyst
2019년 2월 9일
Try adding the colormap and colorbar commands:
numPoints = 500;
y = -100 + 200 * rand(1, numPoints);
x = 40 * rand(1, numPoints);
% Assign colors
cm = [flipud(jet(128)); jet(128)];
% Make 200 long.
cm = imresize(cm, [200, 3]);
cm(cm>1) = 1;
cm(cm<0) = 0;
markerColors = zeros(length(y), 3);
for k = 1 : length(y)
thisValue = round(y(k) + 100);
if thisValue < 1
thisValue = 1;
elseif thisValue > size(cm, 1)
thisValue = size(cm, 1);
end
markerColors(k, :) = cm(thisValue, :);
end
scatter(x, y, 35, markerColors, 'filled');
% Put a line at the x axis
grid on;
line(xlim, [0, 0], 'Color', 'k', 'LineWidth', 2);
colormap(cm);
numTicks = 21;
tickNumbers = linspace(0, 1, numTicks)
for k = 1 : numTicks
tickLabels{k} = sprintf('%.1f', -100 + 200 *tickNumbers(k));
end
colorbar('Ticks', tickNumbers, 'TickLabels', tickLabels);
![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203408/0000%20Screenshot.png)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Colormaps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!