如何在3D图中使用色彩图?(How to use colormap in a 3D plot?)

编程入门 行业动态 更新时间:2024-10-26 01:29:27
如何在3D图中使用色彩图?(How to use colormap in a 3D plot?)

这个问题链接到我以前的帖子 。

考虑以下代码:

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

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

我的问题: 如果我想用“颜色图”来显示与矩阵数对应的颜色,怎么办? 示例:在发布的代码中,我在单元格A有10个矩阵( A{1} , A{2} , A{3} ,..., A{10} ),因此如何使颜色条显示10绘图中使用的颜色以及如何显示1到10的10个数字,对应于绘图中使用的10种颜色(如图所示)?

This question links to my previous post.

Consider the below code:

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

This is the image result from the above code:

MY QUESTION: If I want to use "color map" to show the color corresponding to the number of matrices, how can that be done? Example: In the posted code, I have 10 matrices (A{1}, A{2}, A{3},..., A{10}) inside the cell A, so how to make the colorbar show the 10 colors used in the plot and how to show 10 numbers from 1 to 10 corresponding to the 10 colors used in the plot (as shown in the image)?

最满意答案

在以下代码行中,

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就不会自动识别出你的颜色。 你得到了不同的颜色,因为你正在绘制多个点。


从我之前的答案出发,提出你的实际问题,

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

In the following lines of your code,

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

The fourth input argument of scatter3, which you named C, does not specify color. It is for specifying the size of the circle being plotted. Just because you named it C, MATLAB would not automatically recognise that you meant color. You're getting different colors because you're plotting multiple points with hold on.


Coming towards your actual question and building from 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]);

which gives the following result:

out1

If you want to change the size of the circles as you were mistakenly doing in your code, use the relevant line for plotting which is mentioned in the code. Using that generates the following result:

out2

更多推荐

本文发布于:2023-07-20 12:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图中   色彩   如何在   plot   colormap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!