Overlaying Surface and Vector Plot
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)
Accepted Answer
More Answers (1)
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')
Categories
Find more on Lighting, Transparency, and Shading 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!






