Quiver with color: Add-on

조회 수: 200 (최근 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 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