Quiver with color: Add-on

163 次查看(过去 30 天)
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.

采纳的回答

Adam Danz
Adam Danz 2021-5-12
编辑:Adam Danz 2021-5-12
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 个评论
Niklas Kurz
Niklas Kurz 2021-5-13
quite impressive your code but on the same page it takes quite a little longer to run.
Adam Danz
Adam Danz 2021-5-13
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Vector Fields 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by