如何在 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;
这是上面代码的图像结果:
我的问题:
如果我想用"color map"显示矩阵个数对应的颜色,怎么办?
示例: 在发布的代码中,我有 10 个矩阵(A{1}
、A{2}
、A{3}
、...、A{10}
)在单元格 A
内,那么如何让颜色栏显示绘图中使用的 10 种颜色以及如何显示与绘图中使用的 10 种颜色相对应的从 1 到 10 的 10 个数字(如图所示)?
在您的代码的以下几行中,
C = repmat([i],size(A{i},1),1);%create color matrix C
scatter3(A{i}(:,1),A{i}(:,2),A{i}(:,3),C,'filled');
您命名为 C
的 scatter3
的第四个输入参数未指定颜色。它用于指定正在绘制的圆的大小。仅仅因为您将其命名为 C
,MATLAB 不会自动识别您指的是颜色。你会得到不同的颜色,因为你用 hold on
.
绘制多个点
针对您的实际问题并从 、
构建
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]);
结果如下:
如果您想像在代码中错误地更改圆圈的大小,请使用代码中提到的相关行进行绘图。使用它会生成以下结果:
这个问题链接到
考虑以下代码:
%% 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;
这是上面代码的图像结果:
我的问题:
如果我想用"color map"显示矩阵个数对应的颜色,怎么办?
示例: 在发布的代码中,我有 10 个矩阵(A{1}
、A{2}
、A{3}
、...、A{10}
)在单元格 A
内,那么如何让颜色栏显示绘图中使用的 10 种颜色以及如何显示与绘图中使用的 10 种颜色相对应的从 1 到 10 的 10 个数字(如图所示)?
在您的代码的以下几行中,
C = repmat([i],size(A{i},1),1);%create color matrix C
scatter3(A{i}(:,1),A{i}(:,2),A{i}(:,3),C,'filled');
您命名为 C
的 scatter3
的第四个输入参数未指定颜色。它用于指定正在绘制的圆的大小。仅仅因为您将其命名为 C
,MATLAB 不会自动识别您指的是颜色。你会得到不同的颜色,因为你用 hold on
.
针对您的实际问题并从
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]);
结果如下:
如果您想像在代码中错误地更改圆圈的大小,请使用代码中提到的相关行进行绘图。使用它会生成以下结果: