Search code examples
matlabscatter3d

Matlab: using scatter3 to draw horizontal squares


enter image description here

h1 = scatter3(X_Horiz,Y_Horiz,Z_Horiz,200,'s','filled',...
                           'MarkerEdgeColor','b','MarkerFaceColor',[0 .75 .75]);
hold on;

h2 = scatter3(X_Vert,Y_Vert,Z_Vert,200,'s','filled',...
                           'MarkerEdgeColor','g','MarkerFaceColor',[0 .75 .75]);
hold on;

I know scatter3 can draw vertical square, however, I want to draw horizontal square which is parallel with the light red two interfaces. I try the rotate function, but it does not work.


Solution

  • You can proceed, by drawing your own squares by specifying the length of side. You may follow something like below:

    function myscatter3()
    N = 10 ;
    data = rand(N,2) ;
    x = data(:,1) ; y = data(:,2) ; z = zeros(size(x)) ;
    
    dx = 0.05 ;
    figure
    hold on
    for i = 1:N
        coor = MakeSquare(x(i),y(i),dx) ;
        patch(coor(:,1),coor(:,2),coor(:,3),'w','edgecolor','k') ;
    end
    view(3)    
    end
    
    function coor = MakeSquare(x,y,dx)
    
    coor = zeros(4,3) ;
    
    coor(1,:) = [x-dx/2,y-dx/2,0] ;
    coor(2,:) = [x+dx/2,y-dx/2,0] ;
    coor(3,:) = [x+dx/2,y+dx/2,0] ;
    coor(4,:) = [x-dx/2,y+dx/2,0] ;
    end 
    

    Note that, that code can be fine tuned. It draws squares in the xy plane. It can be generalized to any plane.