如何在MATLAB中个性化轮廓图的颜色图

时间:2019-08-05 10:44:10

标签: matlab

我有2组数据,如果将其相减,将获得系统的绝对性能。因此,将有正值和负值。然后将这些值绘制在等高线图上,以了解哪个值最好。我想使用自己的配色方案对颜色栏进行个性化设置,如下所示:

-10至-2:蓝色, -2至2:白色, 2到10:红色。

我还希望颜色随着幅度的变化而变暗-因此蓝色从-10开始由深蓝色变为浅蓝色至-2,然后在-2和2之间由白色变为白色,从2到10由浅红色变为深红色

我已经设法绘制出等高线图,但是需要更改颜色图。

(MALTAB脚本)

Console.WriteLine("t = " + t);

2 个答案:

答案 0 :(得分:0)

您可以轻松生成自己的颜色图

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);

答案 1 :(得分:0)

我添加此答案,因为此方法与我的第一个答案有很大不同。从长远来看,我认为这更有用,因为它可以扩展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());

enter image description here

请注意,saveAs方法允许保存颜色图以备后用。但是,您看到我为此使用了特定的名称空间,并且您可能想要更改此名称空间。

有关该课程的更多信息,请访问github