【问题标题】:Colour line plot by categorical data in MATLAB在 MATLAB 中按分类数据绘制颜色线图
【发布时间】:2019-08-05 10:13:26
【问题描述】:

我有一个包含 24 个样本和 10,000 个数据点 x 的矩阵,每个数据点具有不同的强度 y,我可以在做 plot(x,y) 的线图中绘制它们,但是我有第三个变量 z (24x1),它是分类的.我正在尝试绘制此图,以使同一类别中的每个样本颜色相同,但尚未起作用。

z 的一个例子是 (A, B, C, A, C, B, ...)。

到目前为止,我已经尝试过

plot(x, y, 'color', z)

但我收到警告:

 Error using plot
 Color value must be a 3 element vector

我在网上找到的所有指南都给出了类似的方法,但没有一个有效。

我知道在 R 中我可以做一些形式的事情

matplot(x, y, color = z)

但我没有使用 matlab 的经验,因此感到困惑。

预期结果是一个图,其中 z 中同一组的每个样本都具有相同的颜色。非常感谢任何帮助。

编辑:

这是一些示例数据。 X 可以只是列号

Sample    1.    2.    3.    4.    5.    Group
1         5     6     6     7     3     A
2.        4     4     6     5     2     B
3.        7     5     4     6     4     A
4.        5     6     3     4     3     C

所以,样品 1 和 3 应该是相同的颜色

【问题讨论】:

  • 由于你没有给出样本数据,所以很难给你一个完整的答案,但我建议你看看这个问题和答案:Change color of 2D plot line depending on 3rd value
  • @Hoki 这个解决方案不适用于我的数据,我现在添加了示例数据

标签: matlab


【解决方案1】:

这段代码应该可以工作

[~,~,types] = unique(category);
colormap jet
cmap = colormap;
con_min = 0;
con_max = max(types);
ind_c = round((size(cmap,1)-1)*types/(con_max-con_min))+1;
set(gca,'ColorOrder',cmap(ind_c,:),'NextPlot','replacechildren');
plot (x, y);

【讨论】:

    【解决方案2】:

    一种方法是为组绘制数据组,在这种情况下,MATLAB 会自动分配不同的颜色:

    samples = [ ...
        5     6     6     7     3
        4     4     6     5     2    
        7     5     4     6     4    
        5     6     3     4     3];    
    
    groups = {...
        'A' 
        'B' 
        'A'
        'C' };
    
    % generate x-values
    x  = repmat(1:size(samples,2), size(samples,1), 1);
    
    
    axes(figure);
    hold on;
    handles = {};
    
    for group = unique(groups')
        idx = (strcmp(groups, group{1}));
        xplot = x(idx,:);
        splot = samples(idx,:);
        handles{end+1} = plot(xplot(:), splot(:),'o');
    end
    

    要访问 Line 对象并更改其属性,例如:

    handles{3}.MarkerFaceColor = 'r';
    

    【讨论】:

      【解决方案3】:

      您需要扫描类别列(您的z 变量)并找到属于每个类别的行的索引。完成后,您可以按组(属于同一类别)绘制线条并分配它们的保留颜色。

      一种方法:

      %% Sample input data
      Y = [   5     6     6     7     3
              4     4     6     5     2    
              7     5     4     6     4    
              5     6     3     4     3 ];    
      
      z = {...
          'A' 
          'B' 
          'A'
          'C' };
      
      x = 1:size(Y,2) ; % just defined to be able to use the notation "plot(x,Y)"
      Y = Y.' ;         % Matlab is column major so transposed the matrix to have
                        % it the natural style (also can use "plot(x,Y)" this way)
      
      %% Assign color for each category
      % define any color you want for each category
      categories_and_colors = { ...
          'A' , [1 0 0] ; % 'Red'     for category 'A'
          'B' , [0 1 0] ; % 'Green'   for category 'B'
          'C' , [0 0 1]   % 'Blue'    for category 'C'
          } ;
      
      %% Find indices of vectors for each category
      ncat = size(categories_and_colors,1) ;
      
      plotcat = cell(ncat,1) ;
      for k=1:ncat
          thisCategory    = categories_and_colors{k,1} ;
          idxThisCategory = strcmpi( thisCategory , z ) ;
          plotcat{k}      = idxThisCategory ;
      end
      % the variable [plotcat] is a cell array which contains a cell per
      % category. Each cell contains the logical indices of the matrix lines
      % belonging to this category
      
      %% Plot
      figure
      hold on
      for k=1:ncat
          plot( x , Y(:,plotcat{k}) , 'Color' , categories_and_colors{k,2} ) ;
      end
      

      当然,一旦您了解了主要流程,就可以压缩代码。 这将产生:

      【讨论】:

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