2

I have the following code that overlays a scatter plot on a surface. Notice that some of the rounded points are chopped off.

figure
scatter(rand(20,1)*10,...
        rand(20,1)*20,...
        'o', 'LineWidth',5, ...
        'MarkerFaceColor', 'black', ...
        'MarkerEdgeColor', 'black')
hold on
[X,Y] = meshgrid(1:0.5:10,1:20);
Z = sin(X) + cos(Y);
surf(X,Y,Z)
view(2)
axis equal square;

Here is the output: enter image description here

mkk
  • 879
  • 6
  • 19
  • I can reproduce (R2017b on Win 10). The effect changes as the window is resized – Luis Mendo May 16 '20 at 00:34
  • shouldn't be a problem if you use two 3D-plotting functions => `scatter3` and make it a 3D plot in the first place – max May 16 '20 at 07:58
  • I know you already have a valid answer, but I feel obliged to link this question to a very similar one : [MATLAB: Drawing atop a surface plot](https://stackoverflow.com/q/40056735/3460361). Have a look there, some answers might interest you too. – Hoki May 18 '20 at 07:37

1 Answers1

3

That's actually not a bug. It happens because you use surf which is a 3D plotting tool, and overlay it with small spheres at the height Z=0 generated by the scatter plot. Some of the spheres are cut by the 2d surface manifold at Z=0, some are below the manifold. Choosing view(2) show the part of the scatter plot that is only above the manifold that surf created (or that the surf Z values are <0) .

Change to view(3) and play with the camera angles to see it:

enter image description here

If you want to overlay a scatter plot on the surf data, you can, but you need to take a different path, here are a few suggestions:

  1. add transparency to the surface plot, for example add alpha(0.5) after the surf line in your code and see...

  2. add an artificial Z value to the surf plot so it will not intersect with the scatter plot at Z=0, for example edit the following line to : Z = sin(X) + cos(Y)-pi; ...

  3. plot it differently (first using imagesc then scatter), for example:

    [X,Y] = meshgrid(1:0.5:10,1:20);
    Z = sin(X) + cos(Y);
    imagesc(1:0.5:10,1:20,Z) % the same meshgrid values are used
    
    hold on 
    scatter(rand(20,1)*10,...
    rand(20,1)*20,...
    'o', 'LineWidth',5, ...
    'MarkerFaceColor', 'black', ...
    'MarkerEdgeColor', 'black')
    
     axis equal square;
    
bla
  • 25,846
  • 10
  • 70
  • 101