Overlaying Surface and Vector Plot

64 views (last 30 days)
In my physics lectures, we often see 2-D plots of vector fields with an underlying surface plot. Currently, I have no issues creating 3-D surface plots or 2-D vector field plots seperately. However, I can't seem to get the two plots overlayed into the diagrams I know from the lectures. I have included three examples below, a single surface plot, a single vector field plot, and my attempts of combining the two. Any help is greatly appreciated!
3-D Surface Plot:
close all
close
[X,Y]=meshgrid(-2:0.1:2,-2:0.1:2);
Z=1000.*exp(-X.^2).*exp(-Y.^2);
surf(X,Y,Z)
2-D Vector Field Plot:
close all
clear
a=10;
[X,Y]=meshgrid(-1:0.1:1,-1:0.1:1);
U=a.*X.*(X.^2 + Y.^2).^(-(3/2));
V=a.*Y.*(X.^2 + Y.^2).^(-(3/2));
quiver(X,Y,U,V)
My attempt at overlaying the two:
close all
clear
a=10;
[X,Y]=meshgrid(-1:0.1:1,-1:0.1:1);
U=a.*X.*(X.^2 + Y.^2).^(-(3/2));
V=a.*Y.*(X.^2 + Y.^2).^(-(3/2));
Z=(X.^2 + Y.^2).^(-(1/2));
hold on
surf(X,Y,Z)
quiver(X,Y,U,V)

Accepted Answer

Star Strider
Star Strider on 21 Apr 2023
Ideally, both plots need to be 3D plots, otherwise the surf plot will completely hide the quiver plot, since the quiver plot has ‘Z=0’ by defnition. One way of getting round this is to use the transparency ('FaceAlpha') of the surf plot to perhaps 0.25, another is to use quiver3 and plot the vector plot on the surface. For best result,s, the same (X,Y) matrices should be used throughout —
[X,Y]=meshgrid(-2:0.1:2,-2:0.1:2);
Z=1000.*exp(-X.^2).*exp(-Y.^2);
figure
surf(X,Y,Z)
% 2-D Vector Field Plot:
% % close all
% % clear
a=10;
% [X,Y]=meshgrid(-1:0.1:1,-1:0.1:1);
U=a.*X.*(X.^2 + Y.^2).^(-(3/2));
V=a.*Y.*(X.^2 + Y.^2).^(-(3/2));
figure
hq = quiver(X,Y,U,V,'r');
figure
surf(X,Y,Z, 'FaceAlpha',0.5, 'EdgeAlpha',0.5)
hold on
quiver(X,Y,U,V,'r', 'LineWidth',2.5)
hold off
view(0,90)
It’s not possible to adjust the quiver ‘ZData’ value to put a ones matrix above the surf plot, so adjusting the surf plot transparency is the only option.. Using quiver3 is another possibility, however this requires defining the ‘W’ matrix to be compatible with the existing quiver matrices and the surf plot. That is likely possible, however doing it here is not straightforward, since I don’t know what it would be in this instance.
I am not certain what you want to do or how you want this to look. This should get you started.
.
  2 Comments
Kyle
Kyle on 21 Apr 2023
First of all, thanks for the detailed answer. I've uploaded an example graphic of what I mean here: https://i.imgur.com/msTBr2P.png. Shown is a test charge q with the vector field equaling the electrical field emitted by the test charge. The underlying surface plot (at least I figured that's what I need to use in MATLAB, correct me if I'm wrong) then shows the strength of the electrical field. You could plot the electrical field and it's strength seperately, but putting both into the same plot is just the best way to portray the information imo, especially for 2+ charges where the vector fields overlap. So I'd say your final result is very close to what I'd want to get. Is there any way for the vectors to be forced into a uniform size? I think (not sure) MATLAB scales the vector arrows to match the intensity? Whereas in other graphics the intensity also scales with color, not size. Just makes it even easier to read.
I'm also not 100% sure if what I'm trying to accomplish is even possible in MATLAB. I've only been playing around with it for about half a year so I'm still very much learning the basics.
Star Strider
Star Strider on 21 Apr 2023
It is apparently not possible to completely disable the arrow scaling, even by setting 'AutoScale','off'. You can experiment tith the various properties as described in Quiver Properties
[X,Y]=meshgrid(-2:0.1:2,-2:0.1:2);
Z=1000.*exp(-X.^2).*exp(-Y.^2);
% figure
% surf(X,Y,Z)
% 2-D Vector Field Plot:
% % close all
% % clear
a=10;
% [X,Y]=meshgrid(-1:0.1:1,-1:0.1:1);
U=a.*X.*(X.^2 + Y.^2).^(-(3/2));
V=a.*Y.*(X.^2 + Y.^2).^(-(3/2));
% figure
% hq = quiver(X,Y,U,V,'r', 0.5);
figure
surf(X,Y,Z, 'FaceAlpha',0.5, 'EdgeAlpha',0.5)
hold on
quiver(X,Y,U,V, 2.5,'r', 'AutoScale','off', 'LineWidth',2.5)
hold off
view(0,90)
.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 21 Apr 2023
Let me add one more command to the code you posted.
a=10;
[X,Y]=meshgrid(-1:0.1:1,-1:0.1:1);
U=a.*X.*(X.^2 + Y.^2).^(-(3/2));
V=a.*Y.*(X.^2 + Y.^2).^(-(3/2));
Z=(X.^2 + Y.^2).^(-(1/2));
hold on
surf(X,Y,Z)
quiver(X,Y,U,V)
view(3)
The view(3) command changes the view of the axes from 2D (in which it was locked by the hold call) to 3D (which it would have been locked into if you'd called hold after surf instead of before. You can see some of the quiver arrows in the z = 0 plane below the surface. By the way you've constructed Z, the entire surface is above the quiver plot. If instead you'd wanted to put the quiver arrows on or near the surface you may want to use quiver3 instead of quiver. Let's make some sample W data.
W = Z;
figure
quiver3(X,Y,Z,U,V,W)
hold on
surf(X,Y,Z)
Why does this plot look so strange? Well, because you included negative exponents in the expressions for U and V those matrices contain some pretty large values near the origin
format longg
U(9:13, 9:13)
ans = 5×5
1.0e+00 * -88.3883476483185 -89.4427190999916 0 89.4427190999916 88.3883476483185 -178.885438199983 -353.553390593274 0 353.553390593274 178.885438199983 -250 -1000 NaN 1000 250 -178.885438199983 -353.553390593274 0 353.553390593274 178.885438199983 -88.3883476483185 -89.4427190999916 0 89.4427190999916 88.3883476483185
V(9:13, 9:13)
ans = 5×5
1.0e+00 * -88.3883476483185 -178.885438199983 -250 -178.885438199983 -88.3883476483185 -89.4427190999916 -353.553390593274 -1000 -353.553390593274 -89.4427190999916 0 0 NaN 0 0 89.4427190999916 353.553390593274 1000 353.553390593274 89.4427190999916 88.3883476483185 178.885438199983 250 178.885438199983 88.3883476483185
The X and Y data you use to position the surface and the tails of the quiver arrows are between -1 and 1. So it may not make sense to plot the surface and the quiver plot on the same axes or you may need to scale the quiver arrows (by say a factor of 1000?)
figure
surf(X, Y, Z)
hold on
scaleFactor = 1/1000;
quiver3(X, Y, Z, U, V, W, scaleFactor, 'r')
  1 Comment
Kyle
Kyle on 21 Apr 2023
Thanks for the answer! I unfortunately haven't dealt with any matrix work in MATLAB yet (haven't needed it for my studies so far), so I'll have to play around with your matrices to understand that stuff a little better (so far they pretty much tell me nothing really). I've uploaded an example graphic of what I'm trying to achieve here: https://i.imgur.com/msTBr2P.png. While the 3-D view looks absolutely phenomenal (didn't know that was possible, we never got to see vector fields in 3-D yet I think), the 2-D view is what I'm mostly after. I get the point of the vectors lying below the surface plot and therefore not being visible on the 2-D plot, would it just suffice to add the scale command to the 2-D plot also?

Sign in to comment.

Categories

Find more on Vector Fields in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!