【问题标题】:How to personalise colour map for a contour plot in MATLAB如何在 MATLAB 中个性化等高线图的颜色图
【发布时间】:2019-08-05 10:44:10
【问题描述】:

我有 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

【问题讨论】:

    标签: matlab


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我添加了这个答案,因为这个方法与我的第一个答案非常不同。从长远来看,我认为这更有用,因为它允许扩展 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-08
        • 1970-01-01
        • 1970-01-01
        • 2012-01-23
        • 2017-12-08
        • 1970-01-01
        相关资源
        最近更新 更多