Overlaying Surface and Vector Plot
71 views (last 30 days)
Show older comments
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)
0 Comments
Accepted Answer
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
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)
.
More Answers (1)
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)
V(9:13, 9:13)
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')
See Also
Categories
Find more on Vector Fields in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!