[英]How to make a contour plot where the colour can be varied independently of the contour height in MATLAB
[英]How to personalise colour map for a contour plot in MATLAB
我有2組數據,如果將其相減,將獲得系統的絕對性能。 因此,將有正值和負值。 然后將這些值繪制在等高線圖上,以了解哪個值最好。 我想使用自己的配色方案對顏色條進行個性化設置,如下所示:
-10至-2:藍色,-2至2:白色,2至10:紅色。
我還希望顏色隨着幅度的變化而變暗-所以藍色從-10開始從深藍色開始,到淺藍色到-2,然后在-2和2之間從白色開始,從2到10從淺紅色到深紅色。
我已經設法繪制出等高線圖,但是需要更改顏色圖。
(MALTAB腳本)
figure
contourf(N_X3,N_Y3,N_Z3,50,'edgecolor','none') %N_X3 is the x axis 1x9 %matrix N_Y3 is the y axis 7x1 matrix and N_Z3 is the z axis 7x9 matrix
title(d);
colormap cool %map colour style
xlabel('Peak Period (t)');
ylabel('Heading (deg)');
c = colorbar('southoutside');
c.Label.String = ('MSI%'); %This is how you label the colourbar
您可以輕松生成自己的顏色圖
bluewhiteredcm = ones(210, 3)
bluewhiteredcm(1:81,1)= (linspace(0,1,81))'
bluewhiteredcm(1:81,2)= (linspace(0,1,81))'
bluewhiteredcm(end-80:end,2)= (linspace(1,0,81))'
bluewhiteredcm(end-80:end,3)= (linspace(1,0,81))'
imagesc(peaks(64));
colormap(bluewhiteredcm);
我添加此答案,因為此方法與我的第一個答案有很大不同。 從長遠來看,我認為這更有用,因為它可以擴展MATLAB提供的顏色圖。 但是,必須使用不同的Colormap Selector等,對於這個問題可能太過分了。
我使用以下類來生成(線性)顏色圖:
classdef LinearColormap < handle
%LINEARCOLORMAP generates a linear colormap
%
% Usage:
% cm = LINEARCOLORMAP(m) generates a m-by-3 colormap
%
% cm.addColor(pos, [r g b]);
% adds the color specified by r g b to the colormap. pos should be a
% relative position. To add at position n (with n<=m) use
% cm.addColor(n/m, ...). If not color is specified for pos=0, black
% is selected, if no color is specified for pos=1, white is selected.
%
% cmap = cm.getCM();
% returns the colormap.
%
% cm.saveAs(filename)
% saves the colormap as a custom colormap in the namespace
% phutils.colormaps.custom. <filename> should be a valid matlab
% function identifier that starts with a lowercase letter
%
% See also: phutils.colormaps.tools.rewriteColormapList
properties (Access = protected)
length
colors
positions
end
methods
function self = LinearColormap( m )
% Generate a LinearColormapObject of size m
self.length = m;
end
function addColor(self, pos, rgb)
% add a color to the colormap
%
% Usage:
%
if any(self.positions == pos)
self.colors(self.positions == pos,:) = rgb;
else
self.colors(end+1,:) = rgb;
self.positions(end+1) = pos;
end
end
function cm = getCM(self)
if ~any(self.positions == 0)
self.addColor(0, [0 0 0]);
end
if ~any(self.positions == 1)
self.addColor(1, [1 1 1]);
end
sorted = sort(self.positions);
idxs = zeros(numel(sorted),1);
for i = 1:numel(sorted)
idxs(i)= find(self.positions == sorted(i));
end
cm = zeros(self.length, 3);
pos = fix(self.positions(idxs) * (self.length-1) ) + 1;
colors = self.colors(idxs,:); %#ok<PROP>
for i = 1:numel(pos)-1
for j = 1:3
cm(pos(i):pos(i+1),j) = ...
linspace(...
colors(i,j),colors(i+1,j),...
pos(i+1)-pos(i)+1 ...
); %#ok<PROP>
end
end
end
function saveAs(self, filename)
% save the current colormap as
% phutils.colormaps.custom.<filename>
if strcmp(filename(1), upper(filename(1)))
error('phutils:WrongArgument', 'Wrong argument type: First letter of filename must be lowercase');
end
fn = mfilename('fullpath');
parts = strsplit(fn, filesep);
path = fullfile(filesep, parts{1:end-1}, '+custom');
if exist(path, 'dir') ~= 7
mkdir (path);
end
fid = fopen(fullfile(path, [filename '.m']),'w');
fprintf(fid, 'function map = %s (m)\n', filename);
fprintf(fid, ' if nargin < 1\n');
fprintf(fid, ' f = get(groot,''CurrentFigure'');\n');
fprintf(fid, ' if isempty(f)\n');
fprintf(fid, ' m = size(get(groot,''DefaultFigureColormap''),1);\n');
fprintf(fid, ' else\n');
fprintf(fid, ' m = size(f.Colormap,1);\n');
fprintf(fid, ' end\n');
fprintf(fid, ' end\n');
fprintf(fid, '\n');
fprintf(fid, ' cm = phutils.colormaps.LinearColormap(m);\n');
for i = 1:numel(self.positions)
fprintf(fid, ' cm.addColor(%d, [%d %d %d]);\n', self.positions(i), self.colors(i,:));
end
fprintf(fid, ' map = cm.getCM();\n');
fprintf(fid, 'end');
fclose(fid);
phutils.colormaps.tools.rewriteColormapList();
end
end
end
用法如下:
bluewhiteredcm = LinearColormap(256);
bluewhiteredcm.addColor(0/21, [0 0 1]);
bluewhiteredcm.addColor(8/21, [1 1 1]);
bluewhiteredcm.addColor(12/21, [1 1 1]);
bluewhiteredcm.addColor(21/21, [1 0 0]);
imagesc(peaks(64));
colormap(bluewhiteredcm.getCM());
請注意, saveAs
方法允許保存顏色圖以備后用。 但是,您看到我為此使用了特定的名稱空間,並且您可能想要更改此名稱空間。
此類的更多信息可以在github上找到
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.