Plot variable across two dimensions
2 views (last 30 days)
Show older comments
It is usual in the field of mechanics of fluids to plot interest variables as function of the thickness and along the length, such as velocity profiles, as:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1367219/image.png)
which shows the velocity profile across the boundary layer in an open flow, or:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1367224/image.png)
which shows the velocity profile of internal flow. This type of plot is extremely helpful to visualize the distribution of the variable across the two dimensions. This is not restricted to velocity components, as temperature may also be shown similarly:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1367229/image.png)
It is clearly in the above that the tempreature distribution is constant at the entrance and parabolic the fully developed region. This type of plot, for instance, is used to show the temperature distribution of lubricant flow in radial bearings, as in:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1367234/image.png)
We can see in this plot the exact distribution of temperature at selected locations of the x axis.
How can one plot such distribution in MATLAB?
Suppose the simplest case:
x = 0:0.2:1; % horizontal distance
y = linspace(0,1,60); % vertical distance
[X,Y] = meshgrid(x,y); % uniform mesh
U = Y.*(1-Y) + X; % parabolic distribution + linear distribution
figure;
subplot(121); quiver(X,Y,U,0*U);
subplot(122); surf(X,Y,U); view(2);
The quiver and surf functions let us visualize the variable distribution on space, but this distribution is not entirely clear either from the quiver or surf plots. We can enhance the quality of the figure in a different way:
figure; contourf(X,Y,U,100,'LineColor','none'); hold on; quiver(X,Y,U,0*U,'k');
And, although the colorscale and the arrows lenght give us an idea of how the distribution of U as function of y is at each x location, I would like to actually plot this distribution. I can plot it at several different tilesets:
figure;
tiledlayout(1,6,"TileSpacing","none");
for ii=1:size(X,2)
nexttile; plot(U(:,ii),Y(:,ii)); xticks([]); yticks([]);
end
But this solution is not good. All plots look the same (we can certainly change the xlim at each plot to scale all plots properly), but one fundamental information is lot: the location of each plot along the x-axis. I even tried to add another axis to the background of this plot, to include the x distance, but this solution was not good, because it is extremely dificutl to align both axes programatically, as the variable may have a negative value, so it is not easy to align the 0 with the desired x position on the axis below. The second difficult with this approach is that the y variable may change in scale, so I don't know how this can be accomplished.
The quiver and surf solutions really are simple. But if the expressions are obtained analitically, how can we plot this distributions as desired?
I though on creating the quiver plot with a fine mesh on the y distribution, without arrow heads, and connect the arrow heads with a line to give me the graph of the variable as function of y. Is this possible?
I am using MATLAB R2021b, but have access to newer versions.
0 Comments
Accepted Answer
Daniel
on 27 Apr 2023
Edited: Daniel
on 27 Apr 2023
The basic concept appears to be interleaving all the lines on the same plot. The key to that is hold on, which lets you superimpose plots on each other on a common axis.
I've included some example code that generates a dummy plot similar to what I think you're looking for. Let me know if there's anything I missed or that requires particular explanation.
t = -5:5; % Length bounds
y = -5:5; % Thickness bounds
f = t.^2/5; % Some function to plot
[tmesh,ymesh] = meshgrid(t,y); % Get matrix forms of t and y
u = -ymesh/10; % Prepare scaled x-displacement of arrows
v = u*0; % No y-displacement of arrows desired
plot(t,f) % Plot the function
hold on % Prepare to plot everything else on the same plot
% Superimpose the quiver on the function plot; 'off' disables autoscaling
% on the arrows; 'ShowArrowHead','off' disables the arrowheads
quiver(t,y,u,v,'off','ShowArrowHead','off');
for idx = 1:length(t)
% Plot each quiver contour at the proper horizontal offset
% 'k' sets the color to black, so all the contour plots have a common
% color
plot(t(idx)+u(:,idx),y,'k')
end
I disabled scaling on the quiver so the contour plots would follow the terminal points on the arrows.
More Answers (0)
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!