Quiver with color: Add-on

199 ビュー (過去 30 日間)
Niklas Kurz
Niklas Kurz 2021 年 5 月 12 日
コメント済み: Adam Danz 2021 年 5 月 13 日
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 Center および File ExchangeVector Fields についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by