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.
댓글 수: 0
채택된 답변
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
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 Exchange에서 Vector Fields에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!