quiver3 对应震级的箭头颜色

quiver3 arrow color corresponding to magnitude

我希望来自 MATLAB 的 quiver3 图中每个箭头的颜色与每个箭头的大小相对应。有什么办法吗?

我在网上看到了一些能够为 2D quiver 执行此操作的示例,但是其中 none 适用于 3D 变体 quiver3

我有以下情节,想用与其大小对应的颜色替换蓝色箭头。

在旧图形系统(R2014a 及更早版本)中,使用 built-in quiver object 是不可能的。您可以轻松获取用于构成 quiver plot

的所有 plot objects
q = quiver(1:5, 1:5, 1:5, 1:5);
handles = findall(q, 'type', 'line');

但是尾巴都用一个图表示object,箭头用另一个图表示。因此,您不能单独更改每个 head/tail 的颜色 .

set(handles(1), 'Color', 'r')
set(handles(2), 'Color', 'g')

但是,随着 HG2(R2014b 及更高版本)的引入,您实际上可以获得 两个(未记录)LineStrip objects( matlab.graphics.primitive.world.LineStrip)(一个代表正面,一个代表反面)。这些可以通过隐藏属性 TailHead.

访问
q = quiver(1, 1, 1, 1);
headLineStrip = q.Head;
tailLineStrip = q.Tail;

然后您可以更改这些 object 的颜色属性以使每个箭头具有不同的颜色。

基本思想

为此,我首先计算所有箭袋的大小(这对 quiverquiver3 都适用)

mags = sqrt(sum(cat(2, q.UData(:), q.VData(:), ...
            reshape(q.WData, numel(q.UData), [])).^2, 2));

然后我使用当前颜色图将每个幅度映射到一个 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);

LineStripColorData属性(当指定为truecolor)也需要有一个 alpha 通道(我们将其设置为 255,表示不透明)。

cmap(:,:,4) = 255;

此时我们可以将 ColorBinding 属性 设置为 interpolated 而不是 object (将其与 quiver object) 并将 q.Headq.TailColorData 属性 设置为我们在上面创建的颜色,为每个箭头赋予它自己的颜色。

完整解决方案

注意: 此解决方案适用于 quiverquiver3 并且代码不必完全适应。

%// 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).');

并应用于二维quiverobject

如果您不一定要将箭头缩放到颜色图的整个范围,您可以使用以下对 histcounts 的调用(而不是上面的行)来使用颜色限制映射大小轴数。

clims = num2cell(get(gca, 'clim'));
[~, ~, ind] = histcounts(mags, linspace(clims{:}, size(currentColormap, 1)));

如果您使用 post 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)

注意:我没有对幅度做任何巧妙的处理,只是根据原始矩阵中的顺序更改每个箭袋的颜色 - 但您应该能够了解如何使用它 "feature"

请注意,如果您使用的是 Suevers 解决方案并且数据中包含 NaN,则应在调用 histcounts 之前包含此行:

mags(isnan(mags)) = [];

否则您将收到有关输入大小错误的错误消息,因为 matlab 不会在您的 U/V/W 数据中为 NaN 创建顶点。