Quiver with color: Add-on

199 vues (au cours des 30 derniers jours)
Niklas Kurz
Niklas Kurz le 12 Mai 2021
Commenté : Adam Danz le 13 Mai 2021
Sadly Matlab didn't enabled color-coded vectors in their ever so neat function quiver
Therefore there are endless detours open in Matlab, like the one I found for plotting what I think of:
It's promissing
"all vectors have the same size based on the optimum value for the grid provided"
Given that I don't understand the function itself I can't work out how . My best try is:
[x,y] = meshgrid(-2:0.2:2);
xr = -y;
yr = x;
ncquiverref(x,y,xr,yr,'col')
What's looking pretty pretty (I mean, look at that clean arrows), but monochrome.

Réponse acceptée

Adam Danz
Adam Danz le 12 Mai 2021
Modifié(e) : Adam Danz le 12 Mai 2021
You can plot each quiver arrow in a loop to individually control the colors. This demo assigns color according to the magnitude of each vector.
% Demo data
[X,Y] = meshgrid(-2:0.2:2);
Z = X .* exp(-X.^2 - Y.^2);
[U,V] = gradient(Z,0.2,0.2);
% Create axes
ax = axes('Color', [.15 .15 .15]);
hold(ax,'on')
box(ax,'on')
% Define the colormap for the quiver arrows.
% cmap can have any number of rows.
cmap = autumn(255);
ax.Colormap = cmap;
% Assign colors based on magnitude of vectors
vectorMagnitude = hypot(U(:),V(:));
% Scale magnitudes to rows of colormap
vecMagNorm = (vectorMagnitude-min(vectorMagnitude))./range(vectorMagnitude);
vecColorIdx = round(vecMagNorm * (size(cmap,1)-1)) + 1;
% Plot the quiver data
for i = 1:numel(Z)
quiver(ax, X(i),Y(i),U(i),V(i), .2, ...
'Color', cmap(vecColorIdx(i),:), 'LineWidth',1)
end
% Set properties for the main axes
axis equal
xlim(ax, [-2 2])
ylim(ax, [-2 2])
% Add colorbar
cb = colorbar(ax);
% Set colorbar range
caxis(ax, [floor(vectorMagnitude(1)), ceil(vectorMagnitude(2))])
% Label the colorbars
ylabel(cb,'Vector magnitude')
  2 commentaires
Niklas Kurz
Niklas Kurz le 13 Mai 2021
quite impressive your code but on the same page it takes quite a little longer to run.
Adam Danz
Adam Danz le 13 Mai 2021
I just looked at the file exchange submission you mentioned and I see that they are using line objects which allows you to set colors differently for each line rather than creating each quiver object individuallywhich is what my approach is doing and is therefore much slower.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Vector Fields dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by