不同大小颗粒的 3D MATLAB 散点图

3D MATLAB scatter plot of particles of different sizes

我正在寻找一种可靠的方法来绘制 3D 绘图粒子,我拥有 3D 坐标和半径:

 x=[0 1 1 0 0 1 1 0]';
 y=[0 0 1 1 0 0 1 1]';
 z=[0 0 0 0 1 1 1 1]';
 radius=[0.1 0.5 0.1 1 1 0.4 0.6 0.2]';

我尝试使用:

 scatter3(x,y,z,4/3*pi.*radius.^3,'filled')

问题:如何以保证相对大小和位置守恒的方式绘制粒子(因此当 window 大小被修改时,粒子看到它们的大小随轴相应调整)?

基本上,我想在 MATLAB 中获得以下图表(我使用 Ovito 从我生成的 .xyz(文本)文件(它包含 [x;y;z;radius])中获得),其中调整图表大小的可能性 window 并且仍然获得相对于颗粒表观大小的适当轴比例):

fourth argument to scatter3 以平方点(1 点 = 1/72 英寸)为单位定义标记区域,这是相对于 figure/screen 大小的,因此与轴数据单位无关.如果您想定义粒子相对于位置数据的半径(这样原点处半径为 1 的粒子将在 x、y 和 z 方向上跨越 [-1 1]),那么 scatter3 是没去上班。

一个选项是将每个粒子绘制为 sphere using surf, as illustrated here。以下是如何使用您的示例数据执行此操作:

% Data and unit sphere surface coordinates:
x = [0 1 1 0 0 1 1 0].';
y = [0 0 1 1 0 0 1 1].';
z = [0 0 0 0 1 1 1 1].';
radius = [0.1 0.5 0.1 1 1 0.4 0.6 0.2].';
[xS, yS, zS] = sphere;

% Plot spheres:
for pt = 1:numel(x)
  surf(x(pt)+xS.*radius(pt), ...  % Shift and scale x data
       y(pt)+yS.*radius(pt), ...  % Shift and scale y data
       z(pt)+zS.*radius(pt), ...  % Shift and scale z data
       'EdgeColor', 'none');
  hold on;
end

% Modify figure and axes:
axis equal
set(gcf, 'Color', 'k');
set(gca, 'Color', 'k', 'Box', 'on', 'BoxStyle', 'full', ...
    'XColor', 'w', 'YColor', 'w', 'ZColor', 'w', 'GridColor', 'none');

这是由此产生的情节:

请注意,表面默认根据高度着色。如果你想给它们不同的颜色,你可以修改表面的 CData and FaceColor 属性。例如,您可以根据半径值(将映射到当前颜色图)为每个表面赋予平面颜色,方法是将上面的 surf 调用修改为:

surf(x(pt)+xS.*radius(pt), ...
     y(pt)+yS.*radius(pt), ...
     z(pt)+zS.*radius(pt), ...
     radius(pt).*ones(size(xS)), ...
     'FaceColor', 'flat', 'EdgeColor', 'none');

好的,找到了。也感谢@gnovice(谢谢伙伴)。下面根据每个粒子的半径选择颜色。

这是代码:

figure
[xS, yS, zS] = sphere;
cmap=parula(length(x));
radius_sort = sort(radius);
% Plot spheres:
for pt = 1:numel(x)
    cmap_ind = find(radius_sort == radius(pt));
  hs=surf(x(pt)+xS.*radius(pt), ...  % Shift and scale x data
       y(pt)+yS.*radius(pt), ...  % Shift and scale y data
       z(pt)+zS.*radius(pt), ...  % Shift and scale z data
       'EdgeColor', 'none');
       set(hs,'FaceColor',cmap(cmap_ind(1),:))
  hold on;
end

还可以添加以下命令来调整坐标轴和图形颜色:

% Modify figure and axes:
axis equal
set(gcf, 'Color', 'k');
set(gca, 'Color', 'k', 'Box', 'on', 'BoxStyle', 'full', ...
    'XColor', 'w', 'YColor', 'w', 'ZColor', 'w', 'GridColor', 'none');