Pra*_*nav 12 matlab matlab-figure
我希望quiver3MATLAB中的每个箭头的颜色对应于每个箭头的大小.有没有办法做到这一点?
我在网上看到了一些能够为2D做到这一点的例子quiver,但是它们都不适用于3D变体quiver3.
我有以下情节,并希望用与其大小相对应的颜色替换蓝色箭头.

Sue*_*ver 21
在旧的图形系统(R2014a和更早版本)中,使用内置quiver对象是不可能的.您可以轻松获取用于构成quiver绘图的所有绘图对象
q = quiver(1:5, 1:5, 1:5, 1:5);
handles = findall(q, 'type', 'line');
Run Code Online (Sandbox Code Playgroud)
但尾部都由一个绘图对象表示,箭头由另一个表示.因此,您无法单独改变每个头/尾的颜色.
set(handles(1), 'Color', 'r')
set(handles(2), 'Color', 'g')
Run Code Online (Sandbox Code Playgroud)
但是,随着HG2(R2014b及更高版本)的引入,您实际上可以访问两个(未记录的)LineStrip对象(matlab.graphics.primitive.world.LineStrip)(一个代表头部,一个代表尾部).这些都是通过隐藏的属性访问Tail和Head.
q = quiver(1, 1, 1, 1);
headLineStrip = q.Head;
tailLineStrip = q.Tail;
Run Code Online (Sandbox Code Playgroud)
然后,您可以更改这些对象的颜色属性,使每个箭头成为不同的颜色.
要做到这一点,我首先计算所有箭的箭袋的幅度(这同时适用于quiver和quiver3)
mags = sqrt(sum(cat(2, q.UData(:), q.VData(:), ...
reshape(q.WData, numel(q.UData), [])).^2, 2));
Run Code Online (Sandbox Code Playgroud)
然后我使用当前的colormap将每个幅度映射到RGB值.最短箭头在色图上分配最低颜色,最长箭头在色图上分配最高颜色.histcounts适用于为每个幅度分配一个索引,该索引可以ind2rgb与色彩映射本身一起传递.我们必须乘以255,因为我们需要将RGB颜色作为8位整数.
% Get the current colormap
currentColormap = colormap(gca);
% Now determine the color to make each arrow using a colormap
[~, ~, ind] = histcounts(mags, size(currentColormap, 1));
% Now map this to a colormap
cmap = uint8(ind2rgb(ind(:), currentColormap) * 255);
Run Code Online (Sandbox Code Playgroud)
的LineStrip ColorData属性(当指定为truecolor)也需要具有alpha通道(我们将设置为255意不透明).
cmap(:,:,4) = 255;
Run Code Online (Sandbox Code Playgroud)
此时我们可以将ColorBinding属性设置为interpolated而不是object(将它与quiver对象分离)并设置ColorData两者的属性q.Head以及q.Tail我们在上面创建的颜色,为每个箭头设置它自己的颜色.
注意:此解决方案适用于两者 quiver,quiver3并且代码不必进行任何调整.
%// Create a quiver3 as we normally would (could also be 2D quiver)
x = 1:10;
y = 1:10;
[X,Y] = meshgrid(x, y);
Z = zeros(size(X));
U = zeros(size(X));
V = zeros(size(X));
W = sqrt(X.^2 + Y.^2);
q = quiver3(X, Y, Z, U, V, W);
%// Compute the magnitude of the vectors
mags = sqrt(sum(cat(2, q.UData(:), q.VData(:), ...
reshape(q.WData, numel(q.UData), [])).^2, 2));
%// Get the current colormap
currentColormap = colormap(gca);
%// Now determine the color to make each arrow using a colormap
[~, ~, ind] = histcounts(mags, size(currentColormap, 1));
%// Now map this to a colormap to get RGB
cmap = uint8(ind2rgb(ind(:), currentColormap) * 255);
cmap(:,:,4) = 255;
cmap = permute(repmat(cmap, [1 3 1]), [2 1 3]);
%// We repeat each color 3 times (using 1:3 below) because each arrow has 3 vertices
set(q.Head, ...
'ColorBinding', 'interpolated', ...
'ColorData', reshape(cmap(1:3,:,:), [], 4).'); %'
%// We repeat each color 2 times (using 1:2 below) because each tail has 2 vertices
set(q.Tail, ...
'ColorBinding', 'interpolated', ...
'ColorData', reshape(cmap(1:2,:,:), [], 4).');
Run Code Online (Sandbox Code Playgroud)
并应用于2D quiver对象
如果您不一定要将箭头缩放到histcounts色彩映射的整个范围,则可以使用以下调用(而不是上面的行)来使用轴的颜色限制来映射大小.
clims = num2cell(get(gca, 'clim'));
[~, ~, ind] = histcounts(mags, linspace(clims{:}, size(currentColormap, 1)));
Run Code Online (Sandbox Code Playgroud)
如果您使用的是r2014b后版本,则可以使用未记录的功能来更改每行和头部的颜色:
figure
[x,y] = meshgrid(-2:.5:2,-1:.5:1);
z = x .* exp(-x.^2 - y.^2);
[u,v,w] = surfnorm(x,y,z);
h=quiver3(x,y,z,u,v,w);
s = size(x);
nPoints = s(1)*s(2);
% create a colour map
cmap = parula(nPoints);
% x2 because each point has 2 points, a start and an end.
cd = uint8(repmat([255 0 0 255]', 1, nPoints*2));
count = 0;
% we need to assign a colour per point
for ii=1:nPoints
% and we need to assign a colour to the start and end of the
% line.
for jj=1:2
count = count + 1;
cd(1:3,count) = uint8(255*cmap(ii,:)');
end
end
% set the colour binding method and the colour data of the tail
set(h.Tail, 'ColorBinding','interpolated', 'ColorData',cd)
% create a color matrix for the heads
cd = uint8(repmat([255 0 0 255]', 1, nPoints*3));
count = 0;
% we need to assign a colour per point
for ii=1:nPoints
% and we need to assign a colour to the all the points
% at the head of the arrow
for jj=1:3
count = count + 1;
cd(1:3,count) = uint8(255*cmap(ii,:)');
end
end
% set the colour binding method and the colour data of the head
set(h.Head, 'ColorBinding','interpolated', 'ColorData',cd)
Run Code Online (Sandbox Code Playgroud)
注意:我没有做任何关于幅度的聪明事情,只是根据原始矩阵中的顺序改变每个箭袋的颜色 - 但是你应该能够理解如何使用这个"特征"