将以下代码复制并保存为gscatter3.m文件,运行gscatter3函数即可。
function [varargout]=gscatter3(x,y,z,g,clr,mrksle,mrksze,mrkfclr,leg,legloc)
%------------------------------说明----------------------------------
% gscatter3 - 按组的 3D 散点图
%
% 用法:
% gscatter3(x,y,z,g);
% gscatter3(x,y,z,g,opt_param);
% opt_param - 可选参数,例如颜色、标记样式、
% 标记大小、标记面颜色、图例和图例位置
% h=gscatter3(...);
%
% 输入参数:
% x, y & z - 相同大小的数据点的向量数组
%
% g - 分组变量要么是字符串的单元格数组,要么是
% 长度等于 x、y 和 z 的长度或一个字符数组
% 不。行数等于 x、y 和 z 的长度。
%
% clr - 着色变量可以是字符串的单元数组或
% 字符数组或长度等于长度的三元素向量
% g 或没有。表示预定义的组或字符串
% 颜色图
% 可能的颜色图名称:
% {'jet','hsv','hot','cool','spring','summer','autumn','winter','gray
% ','骨头','铜','粉色','线条'};
% 可能的颜色字符串:
% {'b','g','r','c','m','y','k'};
%
% mrksle - 标记样式,可以是字符串元胞数组或字符
% 长度等于 g 长度或否的数组。组的
% 可能的标记样式:
%{'+','o','*','.','x','s','d','^','v','<','>','p', 'H'}
%
% mrksze - 标记大小,以点为单位表示大小的标量
%
% mrkfclr - 标记面颜色或者“自动”表示颜色为
% 由 clr 或 'none' 表示;这仅适用于以下
% 标记样式(圆形、方形、菱形、五角星、六角星和
% 四个三角形)
%
% leg - 逻辑 1 或 0 表示图例是否是
% 显示与否
%
% legloc - 图例的位置
% 可能的图例位置:
% North:靠近顶部的内部绘图框
% 南:内底
% 东:内右
% 西:左内
% 东北部:右上角内部
% 西北:左上角内侧
% SouthEast:右下角内侧
% SouthWest:内左下角
% NorthOutside:靠近顶部的外部绘图框
% SouthOutside:外侧底部
% EastOutside:右外
% WestOutside:左侧外侧
% NorthEastOutside:右上角外(默认)
% NorthWestOutside:左上角外侧
% SouthEastOutside:右下角外侧
% SouthWestOutside:左下角外侧
% Best:与图中数据的冲突最少
% BestOutside:地块外最少未使用的空间
%
% 输出参数:
% h - 图中所有线条对象的句柄
%
% 注意:至少需要前三个输入参数。这个功能是
% 与 MATLAB 和 OCTAVE 兼容
%
% 版权:
% V.Salai Selvam
% 副教授兼主任
% 电子与通信工程系
% Sriram 工程学院,Perumalpattu - 602024
% 电子邮件 ID:[email protected]
%------------------------------HELP FINIS----------------------------------
if nargin<4,
disp('Insufficient no of input arguments.');
return;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~isvector(x)||~isvector(y)||~isvector(z),
disp('x, y and z must be vectors.');
return;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (length(x)~=length(y))||(length(y)~=length(z))||(length(z)~=length(x)),
disp('x, y and z must be vectors of same size.');
return;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=x(:)';
y=y(:)';
z=z(:)';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~iscellstr(g),
if ischar(g),
g=cellstr(g)';
else
g=g(:)';
g=cellstr(num2str(g'))';
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if length(g)~=length(x),
disp('If g is a character array, the rows of g must be equal to the length of x, y and z OR');
disp('if g is not a character array, the length of g must be equal to the length of x, y and z.');
return;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gu=unique(g);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if length(gu)>128,
disp('The function can only handle up to 128 groups.');
return;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clrstr={'b','g','r','c','m','y','k'};
clrmaps={'jet','hsv','hot','cool','spring','summer','autumn','winter','gray','bone','copper','pink','lines'};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist('clr','var')||isempty(clr),
if length(gu)<=7,
clr=clrstr(1,1:length(gu));
else
clrmap=colormap([clrmaps{1,1},'(128)']);
inds=fix(linspace(1,128,length(gu)));
clr=clrmap(inds,:);
end;
else
if iscellstr(clr),
if ~isvector(clr),
disp('If clr is a cell array of strings, clr must be a vector.');
return;
end;
clr=clr(:)';
if (length(clr)~=length(g))&&(length(clr)~=length(gu)),
disp('If clr is a cell array of strings, the length of clr must be equal to the length of x, y and z.');
return;
end;
if length(clr)==length(g),
clr=unique(clr);
end;
sm=0;
for i=1:length(clr),
sm=sm+sum(strcmpi(clr{1,i},clrstr));
end;
if sm==0,
disp('Color string must be one of the following supported types:');
disp(clrstr);
return;
end;
elseif ischar(clr),
if ~isvector(clr),
disp('If clr is a character array, clr must be a vector.');
return;
end;
clr=clr(:)';
if sum(strcmpi(clr,clrmaps))~=0,
clrmap=colormap([clr,'(128)']);
inds=fix(linspace(1,128,length(gu)));
clr=clrmap(inds,:);
elseif length(clr)==length(gu),
clr=cellstr(clr')';
sm=0;
for i=1:length(clr),
sm=sm+sum(strcmpi(clr{1,i},clrstr));
end;
if sm==0,
disp('Color string must be one of the following supported types:');
disp(clrstr);
return;
end;
elseif length(clr)==length(g),
clr=unique(clr);
clr=cellstr(clr')';
sm=0;
for i=1:length(clr),
sm=sm+sum(strcmpi(clr{1,i},clrstr));
end;
if sm==0,
disp('Color string must be one of the following supported types:');
disp(clrstr);
return;
end;
else
disp('If clr is a character array, clr must be one of the following supported colormap names:');
disp(colormaps);
disp('OR a character array of the following supported color strings of length equal to');
disp('the length of g or the no. of groups.');
disp(clrstr);
return;
end;
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
mrkslestr={'+','o','*','.','x','s','d','^','v','<','>','p','h'};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist('mrksle','var')||isempty(mrksle),
if length(gu)>length(mrkslestr),
mrksle=repmat(mrkslestr(1,1),1,length(gu));
else
mrksle=mrkslestr(1,1:length(gu));
end;
else
if ~iscellstr(mrksle)&&~ischar(mrksle),
disp('Marker style must be either a cell array of strings or a character array.');
return;
end;
if ~isvector(mrksle),
disp('If mrksle is a cell array of strings or a character array, mrksle must be a vector.');
return;
end;
if iscellstr(mrksle)||ischar(mrksle),
if length(mrksle)~=length(g)&&length(mrksle)~=length(gu),
disp('If mrksle is either a cell array of strings or a character array,');
disp('the length of mrksle must be either of length of g or of length equal to no of groups.');
return;
end;
end;
mrksle=mrksle(:)';
if length(mrksle)==length(g),
mrksle=unique(mrksle);
if ischar(mrksle),
mrksle=cellstr(mrksle')';
end;
else
if ischar(mrksle),
mrksle=cellstr(mrksle')';
end;
end;
sm=0;
for i=1:length(mrksle),
sm=sm+sum(strcmpi(mrksle{1,i},mrkslestr));
end;
if sm==0,
disp('Marker style must be one of the following supported types:');
disp(mrkslestr);
return;
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist('mrksze','var')||isempty(mrksze),
mrksze=5;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~isscalar(mrksze),
disp('Marker size must be a scalar.');
return;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist('mrkfclr','var')||isempty(mrkfclr),
mrkfclr='none';
else
if sum(strcmpi(mrkfclr,{'auto','none'}))==0,
mrkfclr='auto';
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isequal(lower(mrkfclr),'auto'),
mrkfclr=clr;
else
mrkfclr=repmat({mrkfclr},1,length(gu));
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
h=zeros(1,length(gu));
for i=1:length(gu),
inds=strcmpi(gu{1,i},g);
h(1,i)=plot3(x(inds),y(inds),z(inds));
hold on;
if ~iscellstr(clr)&&~ischar(clr),
set(h(1,i),'LineStyle','none','Color',clr(i,:),'Marker',mrksle{1,i},'MarkerSize',mrksze,'MarkerFaceColor',mrkfclr(i,:));
else
set(h(1,i),'LineStyle','none','Color',clr{1,i},'Marker',mrksle{1,i},'MarkerSize',mrksze,'MarkerFaceColor',mrkfclr{1,i});
end;
end;
if ~exist('leg','var')||isempty(leg),
leg=1;
end;
if ~exist('legloc','var')||isempty(legloc),
legloc='NorthEastOutside';
end;
if leg==1,
legend(gu,'Location',legloc);
end;
box on; grid on; hold off;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargout~=0,
varargout={h};
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
来自:
Salai Selvam V (2022). gscatter3 (https://www.mathworks.com/matlabcentral/fileexchange/37970-gscatter3), MATLAB Central File Exchange. Retrieved May 9, 2022.