代码之家  ›  专栏  ›  技术社区  ›  Thanh Ha

如何在3D绘图中使用彩色地图?

  •  0
  • Thanh Ha  · 技术社区  · 8 年前

    my previous post

    考虑以下代码:

    %% How to plot each matrix in a cell in 3d plot(1 matrix with 1 color) ?
    % Generate Sample data cell A(1x10 cell array)
    clear; clc;
    A = cell(1,10); % cell A(1x10 cell array)
    for kk = 1:numel(A)
      z = 10*rand()+(0:pi/50:10*rand()*pi)';
      x = 10*rand()*sin(z);
      y = 10*rand()*cos(z);
      A{kk} = [x,y,z];
    end
    
    % Plot point of each matrix in one figure with different color
    figure
    hold on;
    for i = 1:numel(A)%run i from 1 to length A
    
      C = repmat([i],size(A{i},1),1);%create color matrix C  
      scatter3(A{i}(:,1),A{i}(:,2),A{i}(:,3),C,'filled');
    end
    grid on;
    view(3); % view in 3d plane
    colorbar;
    

    这是上述代码的图像结果:

    output

    我的问题是:

    例子: 在发布的代码中,我有10个矩阵( A{1} A{2} , A{3} ,..., A{10} )牢房内 A

    1 回复  |  直到 8 年前
        1
  •  1
  •   Sardar Usama Francesco.s    8 年前

    C = repmat([i],size(A{i},1),1);%create color matrix C  
    scatter3(A{i}(:,1),A{i}(:,2),A{i}(:,3),C,'filled');
    

    的第四个输入参数 scatter3 C ,不指定颜色。它用于指定正在绘制的圆的大小。就因为你给它起了名字 C ,MATLAB不会自动识别您的意思是颜色。你会得到不同的颜色,因为你用 hold on


    针对您的实际问题,从 my previous answer ,

    newA = vertcat(A{:});                   %Concatenating all matrices inside A vertically
    
    numcolors = numel(A);                   %Number of matrices equals number of colors
    colourRGB = jet(numcolors);             %Generating colours to be used using jet colormap
    colourtimes = cellfun(@(x) size(x,1),A);%Determining num of times each colour will be used
    colourind = zeros(size(newA,1),1);      %Zero matrix with length equals num of points
    colourind([1 cumsum(colourtimes(1:end-1))+1]) = 1;
    colourind = cumsum(colourind);          %Linear indices of colours for newA
    
    scatter3(newA(:,1), newA(:,2), newA(:,3), [] , colourRGB(colourind,:),'filled');
    %However if you want to specify the size of the circles as well as in your
    %original question which you mistakenly wrote for color, use the following line instead:
    % scatter3(newA(:,1), newA(:,2), newA(:,3), colourind , colourRGB(colourind,:),'filled');
    grid on;
    view(3);                                %view in 3d plane 
    colormap(colourRGB);                    %using the custom colormap of the colors we used
    %Adjusting the position of the colorbar ticks
    caxis([1 numcolors]);
    colorbar('YTick',[1+0.5*(numcolors-1)/numcolors:(numcolors-1)/numcolors:numcolors],...
        'YTickLabel', num2str([1:numcolors]'), 'YLim', [1 numcolors]);
    

    out1

    如果您想在代码中错误地更改圆的大小,请使用代码中提到的用于绘制的相关线。使用它可以生成以下结果:

    out2

    推荐文章