✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,
代码获取、论文复现及科研仿真合作可私信。
🍎个人主页:Matlab科研工作室
🍊个人信条:格物致知。
更多Matlab完整代码及仿真定制内容点击👇
🔥 内容介绍
引言
数据可视化是数据科学中至关重要的一环。通过直观地展示数据,我们能够更好地理解数据之间的关系和趋势。在数据可视化领域,3D 散点图边缘图是一种强大的工具,它能够同时展示数据点的分布以及边缘分布的信息。本文将介绍 3D 散点图边缘图的概念、应用和优势。
概述
3D 散点图边缘图是一种用于可视化三维数据的图表类型。它通过在三维空间中绘制散点图,并在图的边缘显示数据的分布情况,提供了全面的数据展示方式。这种图表类型通常用于探索数据的分布特征、检测异常值和观察数据之间的相关性。
应用
3D 散点图边缘图在多个领域中被广泛应用。以下是一些常见的应用场景:
-
科学研究:在物理学、生物学和化学等领域,研究人员经常需要分析多维数据集。通过使用 3D 散点图边缘图,他们能够更好地理解数据点之间的关系和趋势,从而得出有关物理、生物或化学过程的重要结论。
-
金融分析:金融领域的数据通常包含多个维度,如股票价格、市场指数和交易量等。通过使用 3D 散点图边缘图,金融分析师可以更好地观察不同变量之间的关系,发现隐藏在数据中的模式和趋势。
-
地理信息系统:地理信息系统(GIS)是一种用于管理、分析和展示地理数据的工具。在 GIS 中,3D 散点图边缘图被广泛用于可视化地理数据的分布和相关性。例如,研究人员可以使用这种图表类型来分析地震数据,并确定地震活动与地理位置之间的关联。
优势
与传统的二维散点图相比,3D 散点图边缘图具有以下优势:
-
全面展示数据:3D 散点图边缘图不仅能够展示数据点的位置,还能同时展示数据的边缘分布情况。这使得我们能够更全面地理解数据的特征和趋势。
-
观察多变量之间的关系:通过在三维空间中绘制散点图,我们可以观察多个变量之间的关系。这有助于发现变量之间的相关性和模式,进而提供更深入的洞察。
-
发现异常值:3D 散点图边缘图能够帮助我们快速发现数据中的异常值。通过观察边缘分布,我们可以识别出与主要数据集有差异的数据点,从而更好地理解数据的完整性和可靠性。
结论
3D 散点图边缘图是一种强大的数据可视化工具,能够全面展示数据的分布和多变量之间的关系。它在科学研究、金融分析和地理信息系统等领域中被广泛应用,并具有展示数据、观察关系和发现异常值的优势。随着数据科学的不断发展,我们相信 3D 散点图边缘图将在更多领域中发挥重要作用,帮助我们更好地理解和利用数据的价值。
📣 代码
%% How to use scatter3mpdf
clear
clc
close all
% data preparation
N=100;
dat(:,1)=linspace(0,1,N)'+0.1*randn(N,1);
dat(:,2)=linspace(0,1,N)'.^2+0.1*randn(N,1);
dat(:,3)=10*(0.5-linspace(0,1,N))'.^3+0.1*randn(N,1);
% call function with degault option value
scatter3mpdf(dat(:,1),dat(:,2),dat(:,3))
exportgraphics(gcf,'scatter3mpdf_sample.png')
% call function with specified option value
figure
h=scatter3mpdf(dat(:,1),dat(:,2),dat(:,3),...
"mPDFAreaRatio",0.3,...
"FunctionType","cdf","Marker",'s');
% set option values after plotting
set(gca,'fontname','arial')
colormap(cool)
function h=scatter3mpdf(x,y,z,options)
%% h=scatter3mpdf(x,y,z,options)
% SCATTER3MPDF is written by Eiji Konaka, Sep/2023
% This function plotts the followings in one 3-d figure.
% 3-d scatter plot
% 2-d marginalized histogram on x-y, y-z, and z-x planes using PCOLOR
% function. Relative frequency is shown by cell colors.
% 1-d marginalized pdf (or cdf) along x, y, and z axes.
% The plotted pdf is calculated by ksdensity, instead of histogram due to
% technical reasons.
%
% inputs
% x, y, z: data vector with the same dimensions
% options
% mPDFAreaRatio: The size of the ratio of pdf plot area to scatter area
% (default=0.5)
% FunctionType: you can select the type of distribution function from
% 'pdf' or 'cdf' (default='pdf')
% Marker: type of marker of scatter3 function (default='o')
%
% output
% h: handle of figure
% note
% The other options, such as colormap of PCOLOR function, can not be specified
% on calling this function.
arguments
x (:,1) double
y (:,1) double
z (:,1) double
options.mPDFAreaRatio {mustBePositive(options.mPDFAreaRatio)} =0.5
options.FunctionType {mustBeMember(options.FunctionType,{'pdf','cdf'})} = 'pdf'
options.Marker ='o'
end
scatter3(x,y,z,'blue','Marker',options.Marker);hold on;
h=gcf;
xLimVal=get(gca,'xlim');
yLimVal=get(gca,'ylim');
zLimVal=get(gca,'zlim');
cMap=colormap("hot");
cMap=flipud(cMap);
colormap([1 1 1;cMap]);
xlim([0 1]);
switch options.FunctionType
case 'pdf'
[N,c] = hist3([x,y]);
N_pcolor = N'/max(N,[],"all");
N_pcolor(size(N_pcolor,1)+1,size(N_pcolor,2)+1) = 0;
xl = linspace(min(x),max(x),size(N_pcolor,2)); % Columns of N_pcolor
yl = linspace(min(y),max(y),size(N_pcolor,1)); % Rows of N_pcolor
[X,Y,Z] = meshgrid(xl,yl,zLimVal(1));
X=reshape(X, size(N_pcolor));
Y=reshape(Y, size(N_pcolor));
Z=reshape(Z, size(N_pcolor));
surf(X,Y,Z,N_pcolor,'EdgeColor','none',...
'FaceAlpha',0.5);
[N,c] = hist3([x,z]);
N_pcolor = N/max(N,[],"all");
N_pcolor(size(N_pcolor,1)+1,size(N_pcolor,2)+1) = 0;
xl = linspace(min(x),max(x),size(N_pcolor,2)); % Columns of N_pcolor
zl = linspace(min(z),max(z),size(N_pcolor,1)); % Rows of N_pcolor
[X,Y,Z] = meshgrid(xl,yLimVal(2),zl);
X=reshape(X, size(N_pcolor));
Y=reshape(Y, size(N_pcolor));
Z=reshape(Z, size(N_pcolor));
surfc(X,Y,Z,N_pcolor,'EdgeColor','none',...
'FaceAlpha',0.5);
[N,c] = hist3([y,z]);
N_pcolor = N/max(N,[],"all");
N_pcolor(size(N_pcolor,1)+1,size(N_pcolor,2)+1) = 0;
yl = linspace(min(y),max(y),size(N_pcolor,2)); % Columns of N_pcolor
zl = linspace(min(z),max(z),size(N_pcolor,1)); % Rows of N_pcolor
[X,Y,Z] = meshgrid(xLimVal(2),yl,zl);
X=reshape(X, size(N_pcolor));
Y=reshape(Y, size(N_pcolor));
Z=reshape(Z, size(N_pcolor));
surfc(X,Y,Z,N_pcolor,'EdgeColor','none',...
'FaceAlpha',0.5);
[f_pdf_x,xi]=ksdensity(x);
[f_pdf_y,yi]=ksdensity(y);
[f_pdf_z,zi]=ksdensity(z);
plot3(xi, ...
(f_pdf_x-max(f_pdf_x))/(max(f_pdf_x)-min(f_pdf_x)) ...
*(yLimVal(2)-yLimVal(1))*options.mPDFAreaRatio+(yLimVal(1)), ...
min(zLimVal)*ones(size(xi)), ...
'b-');
plot3((f_pdf_y-max(f_pdf_y))/(max(f_pdf_y)-min(f_pdf_y)) ...
*(xLimVal(2)-xLimVal(1))*options.mPDFAreaRatio+(xLimVal(1)), ...
yi, ...
min(zLimVal)*ones(size(xi)), ...
'b-');
plot3((f_pdf_z-max(f_pdf_z))/(max(f_pdf_z)-min(f_pdf_z)) ...
*(xLimVal(2)-xLimVal(1))*options.mPDFAreaRatio+(xLimVal(1)), ...
yLimVal(2)*ones(size(yi)), ...
zi, ...
'b-');
case 'cdf'
[N,c] = hist3([x,y]);
tmp=N;
for n1=1:size(N,1)
for n2=1:size(N,2)
tmp(n1,n2)=sum(sum(N(1:n1, 1:n2)));
end
end
N=tmp;
N_pcolor = N'/max(N,[],"all");
N_pcolor(size(N_pcolor,1)+1,size(N_pcolor,2)+1) = 0;
xl = linspace(min(x),max(x),size(N_pcolor,2)); % Columns of N_pcolor
yl = linspace(min(y),max(y),size(N_pcolor,1)); % Rows of N_pcolor
[X,Y,Z] = meshgrid(xl,yl,zLimVal(1));
X=reshape(X, size(N_pcolor));
Y=reshape(Y, size(N_pcolor));
Z=reshape(Z, size(N_pcolor));
surf(X,Y,Z,N_pcolor,'EdgeColor','none',...
'FaceAlpha',0.5);
[N,c] = hist3([x,z]);
tmp=N;
for n1=1:size(N,1)
for n2=1:size(N,2)
tmp(n1,n2)=sum(sum(N(1:n1, 1:n2)));
end
end
N=tmp;
N_pcolor = N/max(N,[],"all");
N_pcolor(size(N_pcolor,1)+1,size(N_pcolor,2)+1) = 0;
xl = linspace(min(x),max(x),size(N_pcolor,2)); % Columns of N_pcolor
zl = linspace(min(z),max(z),size(N_pcolor,1)); % Rows of N_pcolor
[X,Y,Z] = meshgrid(xl,yLimVal(2),zl);
X=reshape(X, size(N_pcolor));
Y=reshape(Y, size(N_pcolor));
Z=reshape(Z, size(N_pcolor));
surfc(X,Y,Z,N_pcolor,'EdgeColor','none',...
'FaceAlpha',0.5);
[N,c] = hist3([y,z]);
tmp=N;
for n1=1:size(N,1)
for n2=1:size(N,2)
tmp(n1,n2)=sum(sum(N(1:n1, 1:n2)));
end
end
N=tmp;
N_pcolor = N/max(N,[],"all");
N_pcolor(size(N_pcolor,1)+1,size(N_pcolor,2)+1) = 0;
yl = linspace(min(y),max(y),size(N_pcolor,2)); % Columns of N_pcolor
zl = linspace(min(z),max(z),size(N_pcolor,1)); % Rows of N_pcolor
[X,Y,Z] = meshgrid(xLimVal(2),yl,zl);
X=reshape(X, size(N_pcolor));
Y=reshape(Y, size(N_pcolor));
Z=reshape(Z, size(N_pcolor));
surfc(X,Y,Z,N_pcolor,'EdgeColor','none',...
'FaceAlpha',0.5);
[f_cdf_x, x_bins]=ecdf(x);
[f_cdf_y, y_bins]=ecdf(y);
[f_cdf_z, z_bins]=ecdf(z);
plot3(x_bins, ...
(f_cdf_x-1)*(yLimVal(2)-yLimVal(1))*options.mPDFAreaRatio+(yLimVal(1)), ...
min(zLimVal)*ones(size(x_bins)), ...
'b-');
plot3((f_cdf_y-1)*(xLimVal(2)-xLimVal(1))*options.mPDFAreaRatio+(xLimVal(1)), ...
y_bins, min(zLimVal)*ones(size(x_bins)), ...
'b-');
plot3((f_cdf_z-1)*(xLimVal(2)-xLimVal(1))*options.mPDFAreaRatio+(xLimVal(1)), ...
max(yLimVal)*ones(size(y_bins)), z_bins,...
'b-');
end
% 鏋犮伄杩借
plot3([xLimVal(1) xLimVal(1) xLimVal(2) xLimVal(2) xLimVal(1)], ...
[yLimVal(1) -(yLimVal(2)-yLimVal(1))*options.mPDFAreaRatio+(yLimVal(1)) ...
-(yLimVal(2)-yLimVal(1))*options.mPDFAreaRatio+(yLimVal(1)) yLimVal(1) yLimVal(1)], ...
[zLimVal(1) zLimVal(1) zLimVal(1) zLimVal(1) zLimVal(1) ],'k-' ...
)
plot3([-(xLimVal(2)-xLimVal(1))*options.mPDFAreaRatio+(xLimVal(1)) xLimVal(1) ...
xLimVal(1) -(xLimVal(2)-xLimVal(1))*options.mPDFAreaRatio+(xLimVal(1)) -(xLimVal(2)-xLimVal(1))*options.mPDFAreaRatio+(xLimVal(1))], ...
[yLimVal(1) yLimVal(1) yLimVal(2) yLimVal(2) yLimVal(1)], ...
[zLimVal(1) zLimVal(1) zLimVal(1) zLimVal(1) zLimVal(1) ],'k-' ...
)
plot3([-(xLimVal(2)-xLimVal(1))*options.mPDFAreaRatio+(xLimVal(1)) xLimVal(1) ...
xLimVal(1) -(xLimVal(2)-xLimVal(1))*options.mPDFAreaRatio+(xLimVal(1)) -(xLimVal(2)-xLimVal(1))*options.mPDFAreaRatio+(xLimVal(1))], ...
[yLimVal(2) yLimVal(2) yLimVal(2) yLimVal(2) yLimVal(2)], ...
[zLimVal(1) zLimVal(1) zLimVal(2) zLimVal(2) zLimVal(1) ],'k-' ...
)
hold off
end