|
别人的问题,
要画散点图。
只用scatter3,无法调alpha
有一个scatter3sph的函数,思路差不多,一个一个的画球体,可以设置color和size,还有transparent.
比较了两种方式。
都还不错。
- clear all
- clc
- clf
- load data.mat
- x=XYZ2(:,1);
- y=XYZ2(:,2);
- z=XYZ2(:,3);
- v=sqrt(sum((XYZ2(:,[4:6]).^2)'))';
- ind=v<20;
- x(ind)=[];
- y(ind)=[];
- z(ind)=[];
- v(ind)=[];
- n=length(v);
- tm=flipud(jet(140));
- col_tm=tm(5:end-6,:);
- [ba,ind]=sort(v);
- for i=1:n
- col_mm(i,:)=col_tm(floor(v(i))-19,:);
- end
- % sbp_width=0.8;
- % sbp_heig=0.8;
- % n_row = 2;
- % n_col = 2;
- % [out_pos]=fun_mm_subplot_pos(n_row,n_col,sbp_width,sbp_heig);
- % h=figure(1)
- % % set(h, 'Position', [100, 100, 1100, 1000]);
- % set(h, 'Position', [100, 100, 800, 800]);
- % % %potential
- % % ax=axes('position',out_pos(1,:));
- % % scatter3sph(x,y,z,'color',col_mm,'size',log(log(log(v)))/100,'Trans',0.6)
- % scatter3sph(x,y,z,'color',col_mm,'size',log(log(v))/300,'Trans',0.6)
- % grid on
- % axis tight
- % xlabel('x');
- % ylabel('y');
- % zlabel('z');
- % view(30,20)
- % ax=axes('position',out_pos(2,:));
- % scatter3sph(x,y,z,'color',col_mm,'size',log(log(log(v)))/100,'Trans',0.9)
- % grid on
- % xlabel('x');
- % ylabel('y');
- % zlabel('z');
- % view(0,90)
- %
- % ax=axes('position',out_pos(3,:));
- % scatter3sph(x,y,z,'color',col_mm,'size',log(log(log(v)))/100,'Trans',0.9)
- % grid on
- % xlabel('x');
- % ylabel('y');
- % zlabel('z');
- % view(0,0)
- %
- % ax=axes('position',out_pos(4,:));
- % scatter3sph(x,y,z,'color',col_mm,'size',log(log(log(v)))/100,'Trans',0.9)
- % grid on
- % xlabel('x');
- % ylabel('y');
- % zlabel('z');
- % view(90,0)
- % h=gcf;
- % fig_na=['./fig_sphere_3D_single'];
- % fun_work_li_035_myfig_out(h,fig_na,3)
- sbp_width=0.8;
- sbp_heig=0.8;
- n_row = 2;
- n_col = 2;
- [out_pos]=fun_mm_subplot_pos(n_row,n_col,sbp_width,sbp_heig);
- h=figure(1)
- % set(h, 'Position', [100, 100, 1100, 1000]);
- set(h, 'Position', [100, 100, 800,800]);
- maxax= max([range(x), range(y), range(z)]);
- ratios= [range(x)/maxax, range(y)/maxax, range(z)/maxax];
- % %potential
- % ax=axes('position',out_pos(1,:));
- colormap(col_tm)
- h=scatter3(x,y,z,(v)*3,v,...
- 'filled','markeredgecolor','c')
- view(30,20)
- daspect([ratios(1), ratios(2), ratios(3)]);
- xlabel('x');
- ylabel('y');
- zlabel('z');
- axis tight
- %
- % % ax=axes('position',out_pos(2,:));
- % % colormap(col_tm)
- % % h=scatter3(x,y,z,(v)*3,v,...
- % % 'filled','markeredgecolor','c')
- % % view(0,90)
- % % daspect([ratios(1), ratios(2), ratios(3)]);
- % % xlabel('x');
- % % ylabel('y');
- % % zlabel('z');
- % %
- % %
- % % ax=axes('position',out_pos(3,:));
- % % colormap(col_tm)
- % % h=scatter3(x,y,z,(v)*3,v,...
- % % 'filled','markeredgecolor','c')
- % % view(0,0)
- % % daspect([ratios(1), ratios(2), ratios(3)]);
- % % xlabel('x');
- % % ylabel('y');
- % % zlabel('z');
- % %
- % %
- % % ax=axes('position',out_pos(4,:));
- % % colormap(col_tm)
- % % h=scatter3(x,y,z,(v)*3,v,...
- % % 'filled','markeredgecolor','c')
- % % view(90,0)
- % % daspect([ratios(1), ratios(2), ratios(3)]);
- % % xlabel('x');
- % % ylabel('y');
- % % zlabel('z');
- % %
- % %
- h=gcf;
- fig_na=['./fig_circle_3D_sing'];
- fun_work_li_035_myfig_out(h,fig_na,3)
- % % subplot(1,2,2)
- % colormap(col_tm)
- % h=scatter3(x,y,z,(v)*3,v,...
- % 'filled','markeredgecolor','c')
- % maxax= max([range(x), range(y), range(z)]);
- % ratios= [range(x)/maxax, range(y)/maxax, range(z)/maxax];
- % view(0,90)
- % daspect([ratios(1), ratios(2), ratios(3)]);
- % scatter3sph(x(ind),y(ind),z(ind),'color',col_mm,'size',log(v(ind))/1000)
复制代码
- function scatter3sph(X,Y,Z,varargin)
- %SCATTER3SPH (X,Y,Z) Makes a 3d scatter plot with 3D spheres
- % SCATTER3SPH is like scatter3 only drawing spheres instead
- % of flat circles, at coordinates specified by vectors X, Y, Z. All three
- % vectors have to be of the same length.
- % SCATTER3SPH(X,Y,Z) draws the spheres with the default size and color.
- % SCATTER3SPH(X,Y,Z,'size',S) draws the spheres with sizes S. If length(S)= 1
- % the same size is used for all spheres.
- % SCATTER3SPH(X,Y,Z,'color',C) draws the spheres with colors speciffied in a
- % N-by-3 matrix C as RGB values.
- % SCATTER3SPH(X,Y,Z,'transp',T) applies transparency level 'T' to the spheres
- % T= 0 => transparent, T= 1 => opaque.
- % Parameter names can be abreviated to 3 letters. For example: 'siz' or
- % 'col'. Case is irrelevant.
- %
- % Example
- % %Coordinates
- % X= 100*rand(9,1); Y= 100*rand(9,1); Z= 100*rand(9,1);
- %
- % %Colors: 3 blue, 3 red and 3 green
- % C= ones(3,1)*[0 0 1];
- % C= [C;ones(3,1)*[1 0 0]];
- % C= [C;ones(3,1)*[0 1 0]];
- %
- % %Sizes
- % S= 5+10*rand(9,1);
- %
- % scatter3sph(X,Y,Z,'size',S,'color',C,'trans',0.3);
- % axis vis3d
- %-- Some checking...
- if nargin < 3 error('Need at least three arguments'); return; end
- if mean([length(X),length(Y),length(Z)]) ~= length(X) error ('Imput vectors X, Y, Z are of different lengths'); return; end
- %-- Defaults
- C= ones(length(X),1)*[0 0 1];
- S= 0.1*max([X;Y;Z])*ones(length(X),1);
- nfacets= 15;
- transp= 0.5;
- %-- Extract optional arguments
- for j= 1:2:length(varargin)
- string= lower(varargin{j});
- switch string(1:min(3,length(string)))
- case 'siz'
- S= varargin{j+1};
- if length(S) == 1
- S= ones(length(X),1)*S;
- elseif length(S) < length(X)
- error('The vector of sizes must be of the same length as coordinate vectors (or 1)');
- return
- end
- case 'col'
- C= varargin{j+1};
- if size(C,2) < 3 error('Colors matrix must have 3 columns'); return; end
- if size(C,1) == 1
- C= ones(length(X),1)*C(1:3);
- elseif size(C,1) < length(X)
- error('Colors matrix must have the same number of rows as length of coordinate vectors (or 1)');
- return
- end
- case 'fac'
- nfacets= varargin{j+1};
- case 'tra'
- transp= varargin{j+1};
- otherwise
- error('Unknown parameter name. Allowed names: ''size'', ''color'', ''facets'', ''transparency'' ');
- end
- end
- %-- Sphere facets
- [sx,sy,sz]= sphere(nfacets);
- %--- Correct potential distortion
- maxax= max([range(X), range(Y), range(Z)]);
- ratios= [range(X)/maxax, range(Y)/maxax, range(Z)/maxax];
- sx= sx*ratios(1);
- sy= sy*ratios(2);
- sz= sz*ratios(3);
- %-- Plot spheres
- hold on
- for j= 1:length(X)
- surf(sx*S(j)+X(j), sy*S(j)+Y(j), sz*S(j)+Z(j),...
- 'LineStyle','none',...
- 'FaceColor',C(j,:),...
- 'FaceAlpha',transp);
- end
- daspect([ratios(1), ratios(2), ratios(3)]);
- light('Style','infinit','Color',[1 1 1]);
- % light('Position',[1 1 1],'Style','infinit','Color',[1 1 1]);
- % lighting gouraud;
- % view(30,30)
复制代码
|
|