How is it possible to use quiver() to scale the complete vector sets with the same factor for different sets of vectors?

8 views (last 30 days)
I want to give each set of vectors a different color, but the same ''Autoscale" factor. How to do this?
x, y, v_x and v_y have sizes of total x 5 x 2
total = 6;
colors = colormap(lines(total));
for set = 1:total
quiver(x(set, :, 1), y(set, :, 2), v_x(set, :, 1), v_y(set, :, 2),'Color',colors(set, :));
end
In the documentation it is said: When scale is a positive number, the quiver function automatically adjusts the lengths of arrows so they do not overlap, then stretches them by a factor of scale.
This means that for EACH set the lengths of the arrows are adjusted so they don't overlap and THEN stretches them by a factor of scale. However, I want that for the COMPLETE sets of vectors of "set", the length is adjusted so they do not overlap and THEN the scaling is applied to the complete set. So that the scaling makes sense.

Answers (1)

Suraj Kumar
Suraj Kumar on 23 May 2024
Hi Soufyan,
To plot multiple sets of vectors using the quiver function in MATLAB, with each set of vectors having a different color but sharing the same autoscale factor, we can calculate a uniform scale factor instead of letting quiver autoscale each set independently.
The autoscale factor is based on the maximum length of the vectors from all the sets combined, ensuring that the scale is appropriate for all vectors when plotted together.
With the uniform scale factor calculated, we can apply this factor to all sets of vectors by using the AutoScale and AutoScaleFactor properties of the quiver function.
To visually differentiate between each set of vectors, we use a colormap to assign a unique color to each set.
For further clarity, you might find the following code snippet useful:
colors = colormap(lines(total));
maxLen = 0;
for set = 1:total
lengths = sqrt(v_x(set, :, 1).^2 + v_y(set, :, 2).^2);
maxLen = max(maxLen, max(lengths(:)));
end
scale = 1 / maxLen;
% Now, plot each set of vectors with the uniform scaling
hold on;
for set = 1:total
quiver(x(set, :, 1), y(set, :, 2), v_x(set, :, 1), v_y(set, :, 2), 'Color', colors(set, :), 'AutoScale', 'off', 'AutoScaleFactor', scale);
end
hold off;
To know more about quiver and colormap you can go through the below mentioned documentations:
  1. https://www.mathworks.com/help/matlab/ref/quiver.html
  2. https://www.mathworks.com/help/matlab/ref/colormap.html

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!