How to standardize the colormap of every figure created from different set of data

18 views (last 30 days)
i want to generate figures with the same color at the background (as you can see the current figures i have generated have different color intensitiy because of different range of data used). My current simple basic codes are below:
h = figure();
surf(SMImage{SMImageNumber},'edgecolor','none');
colormap(jet(256));
colorbar;
So, how can i standard the colormap so that every figures generated will have the same color background (preferably in blue, because thats what i commonly noticed in previous work) ?

Answers (1)

Robert U
Robert U on 26 May 2020
Edited: Robert U on 26 May 2020
Hi NUR AMIRA ZULKIFLLI,
you can supply own colormaps to several plots. You might want to look into imagesc() since it provides limits as well.
[X,Y,Z] = peaks(100);
dLim = [-4, 4];
newFh = figure;
ah = axes(newFh);
imagesc(ah,X(1,1:end),Y(1,1:end),Z,dLim);
cbh = colorbar(ah);
colormap(ah,cmStd([0,0,1],[1,1,1],[1,0,0],256,false))
cmStd:
function cmap = cmStd(firstColor,midColor,endColor,n,binv)
% cmStd - returns a colormap array
%
% generates an Nx3 array of the RGB values of a colormap that ranges
% from first color at the low end, midColor in the middle, and endColor at the high
% end. The colormap can be inverted.
%
% USAGE
% cmap = cmStd([0,0,1],[1,1,1],[1,0,0],256,false) % blue to red over white
%
% INPUTS
% firstColor - rgb triplet, color of lowest value
% midColor - rgb triplet, color of lowest value
% endColor - rgb triplet, color of lowest value
% n - (optional) the number of bins in the colormap.
% Default is 64.
% binv - (optional) logical, invert colors
% Default is false.
%
% OUTPUTS
% cmap - the Nx3 colormap that can be used with the 'colormap' command
if nargin <=3
n = 64;
binv = false;
elseif nargin == 4
binv = false;
elseif nargin > 5
error('Too many inputs. Expected up to four, got %d',nargin)
end
validateattributes(firstColor,{'double'},{'size',[1 3],'<=',1,'>=',0});
validateattributes(midColor,{'double'},{'size',[1 3],'<=',1,'>=',0});
validateattributes(endColor,{'double'},{'size',[1 3],'<=',1,'>=',0});
validateattributes(n,{'double'},{'integer','scalar','>=',3});
validateattributes(binv,{'logical'},{'nonempty','scalar'});
% set the x, y, and z valules and interpolate
x = [1,n/2,n];
y = 1:3;
xnew = 1:n;
ynew = 1:3;
[x,y] = meshgrid(x,y);
[xnew,ynew] = meshgrid(xnew,ynew);
z(:,1) = firstColor';
z(:,2) = midColor';
z(:,3) = endColor';
cmap = interp2(x,y,z,xnew,ynew)';
if binv
cmap = flip(cmap,1);
end
end
Kind regards,
Robert

Categories

Find more on Colormaps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!