Vector magnitude with colors
75 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
JACK LONDON
am 31 Dez. 2022
Kommentiert: Image Analyst
am 1 Jan. 2023
I want to print velocity vectors on Matlab. I use function "quiver".
It satisfies to draw velocity vectors but I can t give color to the vectors.
I try to give color to velocity vectors depend on magnitude of vectors like this image
Which code I need to color velocity vectors with colorbar like above image? Thank you.
This is my codes:
xyz=dlmread('vector3dn.txt');
X=xyz(:,1);
Y=xyz(:,2);
Z=xyz(:,3);
U=xyz(:,4);
V=xyz(:,5);
W=xyz(:,6);
quiver3(X,Y,Z,U,V,W)
axis equal
Note : I uploaded my source file as an attachment.
0 Kommentare
Akzeptierte Antwort
Voss
am 31 Dez. 2022
Bearbeitet: Voss
am 31 Dez. 2022
As far as I can tell, there is no way to create a quiver plot (or quiver3 plot) with quivers of different colors.
One way to approach this problem is to create one quiver3 plot (of a given color) for each color in the colormap (i.e., the colors depicted in the colorbar). Now, in quiver/3 the quivers are scaled in length so as not to overlap each other (with an additional optional scale factor), but the only way (as far as I can tell) to get multiple quiver/3 plots to use the same scale factor is to turn that scaling off. So that could work, scaling the magnitude data down by an appropriate amount before sending it to quiver/3:
% xyz=dlmread('vector3dn.txt');
xyz = readmatrix('vector3dn.txt');
% scale the u,v,w components of the vectors down, for nice plotting
sf = 0.1;
xyz(:,4:6) = xyz(:,4:6)*sf;
% calculate the magnitudes of the vectors
magnitude = sqrt(sum(xyz(:,4:6).^2,2));
% calculate the (rounded) min and max magnitude
mag_res = 0.05;
mlim = [ ...
floor(min(magnitude)/mag_res) ...
ceil(max(magnitude)/mag_res) ...
]*mag_res;
% define a color map
n_colors = 64;
cmap = autumn(n_colors);
% calculate the magnitude thresholds, between each pair of which the
% quivers will be one color
mthresholds = linspace(mlim(1),mlim(2),n_colors+1);
% create a figure, and go ahead and hold on for multiple plots
figure
hold on
% for each color
for ii = 1:n_colors
% find the indicies of the magnitudes at this color level
idx = magnitude >= mthresholds(ii) & magnitude < mthresholds(ii+1);
% construct the x,y,z,u,v,w arguments for quiver3 corresponding
% to those magnitudes within this level
args = num2cell(xyz(idx,:),1);
% create the quiver3 plot of the right color, with no auto-scaling
quiver3(args{:},'off','Color',cmap(ii,:))
end
% set some axes properties
box on
grid on
axis equal
view(3);
set(gca(),'Color','k')
% set up the colorbar
colormap(cmap);
colorbar();
caxis(mlim/sf);
Or you can use line objects instead of quivers; the approach is the same - splitting it up into multiple sets of lines, one for each color. You just have to calculate the "other" end of each line (x+u, y+v, z+w).
% xyz=dlmread('vector3dn.txt');
xyz = readmatrix('vector3dn.txt');
% calculate the magnitudes of the vectors
magnitude = sqrt(sum(xyz(:,4:6).^2,2));
% calculate the (rounded) min and max magnitude
mag_res = 0.05;
mlim = [ ...
floor(min(magnitude)/mag_res) ...
ceil(max(magnitude)/mag_res) ...
]*mag_res;
% define a color map:
n_colors = 64;
cmap = autumn(n_colors);
% calculate the magnitude thresholds, between each pair of which the
% lines will be one color
mthresholds = linspace(mlim(1),mlim(2),n_colors+1);
% create a figure
figure
% scale factor, for plotting
sf = 0.1;
% for each color
for ii = 1:n_colors
% find the indicies of the magnitudes at this color level
idx = magnitude >= mthresholds(ii) & magnitude < mthresholds(ii+1);
n = nnz(idx);
% construct the x, y, z coordinates of the lines [x x+u], [y y+v], [z z+w]
x = xyz(idx,1)+[zeros(n,1) sf*xyz(idx,4)];
y = xyz(idx,2)+[zeros(n,1) sf*xyz(idx,5)];
z = xyz(idx,3)+[zeros(n,1) sf*xyz(idx,6)];
% create the lines with the right color
line(x.',y.',z.','Color',cmap(ii,:))
end
% set some axes properties
box on
grid on
axis equal
view(3);
set(gca(),'Color','k')
% set up the colorbar
colormap(cmap);
colorbar();
caxis(mlim);
4 Kommentare
Image Analyst
am 1 Jan. 2023
See the colormap documentation for some preset colormaps. You might like winter instead of autumn:
n_colors = 8; % Whatever
cmap = winter(n_colors) % Blue to green
or you can make your own one of pure blues:
n_colors = 8; % Whatever
cmap = [zeros(n_colors, 1), zeros(n_colors, 1), linspace(0, 1, n_colors)'] % Pure blue
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Vector Fields finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!