How to match the colors in the colorbars across two plots such that the datapoints can be inferred consistently?

7 views (last 30 days)
I have generated these figures that have 2 colorbars for the respective plots. I would like to change the colorbar of the leftdisplay such that it spans from blue to yellow, instead of blue to red. This would make reading the data consistent across the two plots. i.e. a value of 2 in the right display would be the same color as the left display. How do I change the span of the colorbar in the left display such that the colors are consistent across the 2 colorbars?
I am using Matlab2022b in a windows machine if that matters.

Answers (2)

DGM
DGM on 11 Aug 2023
Edited: DGM on 11 Aug 2023
This depends whether you can rely on the range of the second dataset being smaller than (and within) the range of the second dataset.
The simplest suggestion would be to use clim() or caxis() (depending on your version) to get/set the scale limits such that they're the same. When doing this, the two colorbars will show the same range of colors and values.
% some fake data with slightly different ranges
z1 = rand(10);
z2 = rand(10)*0.6; % second range is within the first
CT = jet(256);
% one plot
figure(1)
imagesc(z1)
colorbar
colormap(CT)
originalcscale = caxis(gca); % get the color axis limits from this plot
% another plot
figure(2)
imagesc(z2)
colorbar;
colormap(CT)
caxis(originalcscale); % make the color axis limits the same as the first plot
If it's the first range that's smaller, or if they don't completely overlap, then you'll need to set caxis() to the union of the two ranges (like in the following example). Otherwise, you'll end up with potentially large parts of one or both plots being clamped at one end color or the other.
If instead of having the colorbars identical, you want the colorbars to only show the relevant segment of a common colormap, things get more complicated. At this point, I'm going to assume that the ranges of the two datasets can be anything relative to each other.
% these fake datasets overlap, but neither range spans the other
z1 = rand(10)*0.7;
z2 = rand(10)*0.7 + 0.3;
% get ranges
datarange = [imrange(z1); imrange(z2)]; % the global min and max of each data set
urg = imrange(datarange); % the union of data ranges
% build color table segments
ctlength = 256; % use a long table
CT = jet(ctlength); % the base colormap
CTsegments = cell(2,1);
for k = 1:2
ctidx = interp1(urg,[1 ctlength],datarange(k,:),'linear','extrap');
ctidx = round(ctidx);
CTsegments{k} = CT(ctidx(1):ctidx(2),:); % extract a segment
% this could also be done by interpolating the CT itself,
% but let's start simple
end
% one plot
figure(1)
imagesc(z1)
colorbar
colormap(CTsegments{1})
% another plot
figure(2)
imagesc(z2)
colorbar
colormap(CTsegments{2})
There are probably other ways, but that's what I slapped together.

dpb
dpb on 11 Aug 2023
Be easier if you had attached the code to generate the figures so could mung on it directly, but something like
% create first figure here
...
hCB(1)=colorbar; % then add the colorbar; save its handle
...
% create second figure here
...
hCB(2)=colorbar('Limits',hCB(1).Limits); % set limits of second to match first

Tags

Community Treasure Hunt

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

Start Hunting!