How to standardize the colormap of every figure created from different set of data
11 次查看(过去 30 天)
显示 更早的评论
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) ?![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/301193/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/301193/image.png)
回答(1 个)
Robert U
2020-5-26
编辑:Robert U
2020-5-26
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/301388/image.jpeg)
Kind regards,
Robert
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!