65 views (last 30 days)
Show older comments
JACK LONDON on 31 Dec 2022
Commented: Image Analyst on 1 Jan 2023
Accepted Answer: Voss
- vector3dn.txt
Open in MATLAB Online
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 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Voss on 31 Dec 2022
Edited: Voss on 31 Dec 2022
- vector3dn.txt
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 Comments Show 2 older commentsHide 2 older comments
Show 2 older commentsHide 2 older comments
JACK LONDON on 1 Jan 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#comment_2542162
Is there a way set backround color as a white instead of black? Thank you.
Voss on 1 Jan 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#comment_2542172
Open in MATLAB Online
Sure, just delete this line:
set(gca(),'Color','k')
JACK LONDON on 1 Jan 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#comment_2542177
Thank you. As a last question could be range of the colors blue tones instead of yelllow-red colors?
Image Analyst on 1 Jan 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/1886852-vector-magnitude-with-colors#comment_2542327
Open in MATLAB Online
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
cmap = 8×3
0 0 1.0000 0 0.1429 0.9286 0 0.2857 0.8571 0 0.4286 0.7857 0 0.5714 0.7143 0 0.7143 0.6429 0 0.8571 0.5714 0 1.0000 0.5000
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
cmap = 8×3
0 0 0 0 0 0.1429 0 0 0.2857 0 0 0.4286 0 0 0.5714 0 0 0.7143 0 0 0.8571 0 0 1.0000
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and AnnotationColormapsRed
Find more on Red in Help Center and File Exchange
Tags
- vectors
- matlab
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office