Mathematica中文论坛-非官方

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 8990|回复: 0
打印 上一主题 下一主题

豆粑粑 3D sphere画scatter3散点图

[复制链接]

550

主题

629

帖子

3181

积分

论坛元老

Rank: 8Rank: 8

积分
3181
跳转到指定楼层
楼主
发表于 2017-4-11 11:10:20 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
别人的问题,
要画散点图。
只用scatter3,无法调alpha

有一个scatter3sph的函数,思路差不多,一个一个的画球体,可以设置color和size,还有transparent.
比较了两种方式。
都还不错。

  1. clear all
  2. clc
  3. clf

  4. load data.mat
  5. x=XYZ2(:,1);
  6. y=XYZ2(:,2);
  7. z=XYZ2(:,3);
  8. v=sqrt(sum((XYZ2(:,[4:6]).^2)'))';
  9. ind=v<20;
  10. x(ind)=[];
  11. y(ind)=[];
  12. z(ind)=[];
  13. v(ind)=[];
  14. n=length(v);

  15. tm=flipud(jet(140));
  16. col_tm=tm(5:end-6,:);

  17. [ba,ind]=sort(v);
  18. for i=1:n
  19.     col_mm(i,:)=col_tm(floor(v(i))-19,:);
  20. end

  21. % sbp_width=0.8;
  22. % sbp_heig=0.8;
  23. % n_row = 2;
  24. % n_col = 2;
  25. % [out_pos]=fun_mm_subplot_pos(n_row,n_col,sbp_width,sbp_heig);
  26. % h=figure(1)
  27. % % set(h, 'Position', [100, 100, 1100, 1000]);
  28. % set(h, 'Position', [100, 100, 800, 800]);
  29. % % %potential
  30. % % ax=axes('position',out_pos(1,:));
  31. % % scatter3sph(x,y,z,'color',col_mm,'size',log(log(log(v)))/100,'Trans',0.6)
  32. % scatter3sph(x,y,z,'color',col_mm,'size',log(log(v))/300,'Trans',0.6)
  33. % grid on
  34. % axis tight
  35. % xlabel('x');
  36. % ylabel('y');
  37. % zlabel('z');
  38. % view(30,20)

  39. % ax=axes('position',out_pos(2,:));
  40. % scatter3sph(x,y,z,'color',col_mm,'size',log(log(log(v)))/100,'Trans',0.9)
  41. % grid on
  42. % xlabel('x');
  43. % ylabel('y');
  44. % zlabel('z');
  45. % view(0,90)
  46. %
  47. % ax=axes('position',out_pos(3,:));
  48. % scatter3sph(x,y,z,'color',col_mm,'size',log(log(log(v)))/100,'Trans',0.9)
  49. % grid on
  50. % xlabel('x');
  51. % ylabel('y');
  52. % zlabel('z');
  53. % view(0,0)
  54. %
  55. % ax=axes('position',out_pos(4,:));
  56. % scatter3sph(x,y,z,'color',col_mm,'size',log(log(log(v)))/100,'Trans',0.9)
  57. % grid on
  58. % xlabel('x');
  59. % ylabel('y');
  60. % zlabel('z');
  61. % view(90,0)

  62. % h=gcf;
  63. % fig_na=['./fig_sphere_3D_single'];
  64. % fun_work_li_035_myfig_out(h,fig_na,3)


  65. sbp_width=0.8;
  66. sbp_heig=0.8;
  67. n_row = 2;
  68. n_col = 2;
  69. [out_pos]=fun_mm_subplot_pos(n_row,n_col,sbp_width,sbp_heig);
  70. h=figure(1)
  71. % set(h, 'Position', [100, 100, 1100, 1000]);
  72. set(h, 'Position', [100, 100, 800,800]);


  73. maxax= max([range(x), range(y), range(z)]);
  74. ratios= [range(x)/maxax, range(y)/maxax, range(z)/maxax];

  75. % %potential
  76. % ax=axes('position',out_pos(1,:));
  77. colormap(col_tm)
  78. h=scatter3(x,y,z,(v)*3,v,...
  79.     'filled','markeredgecolor','c')
  80. view(30,20)
  81. daspect([ratios(1), ratios(2), ratios(3)]);
  82. xlabel('x');
  83. ylabel('y');
  84. zlabel('z');
  85. axis tight
  86. %
  87. % % ax=axes('position',out_pos(2,:));
  88. % % colormap(col_tm)
  89. % % h=scatter3(x,y,z,(v)*3,v,...
  90. % %     'filled','markeredgecolor','c')
  91. % % view(0,90)
  92. % % daspect([ratios(1), ratios(2), ratios(3)]);
  93. % % xlabel('x');
  94. % % ylabel('y');
  95. % % zlabel('z');
  96. % %
  97. % %
  98. % % ax=axes('position',out_pos(3,:));
  99. % % colormap(col_tm)
  100. % % h=scatter3(x,y,z,(v)*3,v,...
  101. % %     'filled','markeredgecolor','c')
  102. % % view(0,0)
  103. % % daspect([ratios(1), ratios(2), ratios(3)]);
  104. % % xlabel('x');
  105. % % ylabel('y');
  106. % % zlabel('z');
  107. % %
  108. % %
  109. % % ax=axes('position',out_pos(4,:));
  110. % % colormap(col_tm)
  111. % % h=scatter3(x,y,z,(v)*3,v,...
  112. % %     'filled','markeredgecolor','c')
  113. % % view(90,0)
  114. % % daspect([ratios(1), ratios(2), ratios(3)]);
  115. % % xlabel('x');
  116. % % ylabel('y');
  117. % % zlabel('z');
  118. % %
  119. % %
  120. h=gcf;
  121. fig_na=['./fig_circle_3D_sing'];
  122. fun_work_li_035_myfig_out(h,fig_na,3)
  123. % % subplot(1,2,2)
  124. % colormap(col_tm)
  125. % h=scatter3(x,y,z,(v)*3,v,...
  126. %     'filled','markeredgecolor','c')
  127. % maxax= max([range(x), range(y), range(z)]);
  128. % ratios= [range(x)/maxax, range(y)/maxax, range(z)/maxax];
  129. % view(0,90)
  130. % daspect([ratios(1), ratios(2), ratios(3)]);


  131. % scatter3sph(x(ind),y(ind),z(ind),'color',col_mm,'size',log(v(ind))/1000)
复制代码


  1. function scatter3sph(X,Y,Z,varargin)
  2. %SCATTER3SPH (X,Y,Z) Makes a 3d scatter plot with 3D spheres
  3. %        SCATTER3SPH is like scatter3 only drawing spheres instead
  4. %                of flat circles, at coordinates specified by vectors X, Y, Z. All three
  5. %                vectors have to be of the same length.
  6. %        SCATTER3SPH(X,Y,Z) draws the spheres with the default size and color.
  7. %        SCATTER3SPH(X,Y,Z,'size',S) draws the spheres with sizes S. If length(S)= 1
  8. %                the same size is used for all spheres.
  9. %        SCATTER3SPH(X,Y,Z,'color',C) draws the spheres with colors speciffied in a
  10. %                N-by-3 matrix C as RGB values.
  11. %        SCATTER3SPH(X,Y,Z,'transp',T) applies transparency level 'T' to the spheres
  12. %                T= 0 => transparent, T= 1 => opaque.
  13. %        Parameter names can be abreviated to 3 letters. For example: 'siz' or
  14. %                'col'. Case is irrelevant.
  15. %
  16. % Example
  17. % %Coordinates
  18. %  X= 100*rand(9,1); Y= 100*rand(9,1); Z= 100*rand(9,1);
  19. %
  20. % %Colors: 3 blue, 3 red and 3 green
  21. % C= ones(3,1)*[0 0 1];
  22. % C= [C;ones(3,1)*[1 0 0]];
  23. % C= [C;ones(3,1)*[0 1 0]];
  24. %
  25. % %Sizes
  26. % S= 5+10*rand(9,1);
  27. %
  28. % scatter3sph(X,Y,Z,'size',S,'color',C,'trans',0.3);
  29. % axis vis3d


  30. %-- Some checking...
  31. if nargin < 3 error('Need at least three arguments'); return; end
  32. if mean([length(X),length(Y),length(Z)]) ~= length(X) error ('Imput vectors X, Y, Z are of different lengths'); return; end

  33. %-- Defaults
  34. C= ones(length(X),1)*[0 0 1];
  35. S= 0.1*max([X;Y;Z])*ones(length(X),1);
  36. nfacets= 15;
  37. transp= 0.5;


  38. %-- Extract optional arguments
  39. for j= 1:2:length(varargin)
  40.         string= lower(varargin{j});
  41.         switch string(1:min(3,length(string)))
  42.                 case 'siz'
  43.                         S= varargin{j+1};
  44.                         if length(S) == 1
  45.                                 S= ones(length(X),1)*S;
  46.                         elseif length(S) < length(X)
  47.                                 error('The vector of sizes must be of the same length as coordinate vectors (or 1)');
  48.                                 return
  49.                         end

  50.                 case 'col'
  51.                         C= varargin{j+1};
  52.                         if size(C,2) < 3        error('Colors matrix must have 3 columns'); return; end
  53.                         if size(C,1) == 1
  54.                                 C= ones(length(X),1)*C(1:3);
  55.                         elseif size(C,1) < length(X)
  56.                                 error('Colors matrix must have the same number of rows as length of coordinate vectors (or 1)');
  57.                                 return
  58.                         end

  59.                         case 'fac'
  60.                                 nfacets= varargin{j+1};

  61.                         case 'tra'
  62.                                 transp= varargin{j+1};

  63.                 otherwise
  64.                         error('Unknown parameter name. Allowed names: ''size'', ''color'', ''facets'', ''transparency'' ');
  65.         end
  66. end


  67. %-- Sphere facets
  68. [sx,sy,sz]= sphere(nfacets);


  69. %--- Correct potential distortion
  70. maxax= max([range(X), range(Y), range(Z)]);
  71. ratios= [range(X)/maxax, range(Y)/maxax, range(Z)/maxax];
  72. sx= sx*ratios(1);
  73. sy= sy*ratios(2);
  74. sz= sz*ratios(3);


  75. %-- Plot spheres
  76. hold on
  77. for j= 1:length(X)
  78.         surf(sx*S(j)+X(j), sy*S(j)+Y(j), sz*S(j)+Z(j),...
  79.                 'LineStyle','none',...
  80.                 'FaceColor',C(j,:),...
  81.                 'FaceAlpha',transp);
  82. end

  83. daspect([ratios(1), ratios(2), ratios(3)]);
  84. light('Style','infinit','Color',[1  1  1]);
  85. % light('Position',[1 1 1],'Style','infinit','Color',[1 1 1]);
  86. % lighting gouraud;
  87. % view(30,30)
复制代码


fig_circle_3D_.png (603.95 KB, 下载次数: 1432)

fig_circle_3D_.png

fig_sphere_3D.png (394.15 KB, 下载次数: 1296)

fig_sphere_3D.png
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|Mathematica中文论坛-非官方 ( 辽ICP备16001491号-1

GMT+8, 2024-12-28 15:07 , Processed in 0.102961 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表