Matlab 箭筒3与震级对应的箭头颜色
我想让MATLAB中的Matlab 箭筒3与震级对应的箭头颜色,matlab,matlab-figure,Matlab,Matlab Figure,我想让MATLAB中的quiver3绘图中每个箭头的颜色与每个箭头的大小相对应。有办法吗 我在网上看到了一些能够为2Dquiver实现这一点的示例,但是它们都不适用于3D变体quiver3 我有下面的图,我想用与它们的大小相对应的颜色来替换蓝色箭头 在旧的图形系统(R2014a及更早版本)中,这不可能使用内置的quiver对象。您可以轻松获得用于构成quiver绘图的所有绘图对象 q = quiver(1:5, 1:5, 1:5, 1:5); handles = findall(q, 'type
quiver3
绘图中每个箭头的颜色与每个箭头的大小相对应。有办法吗
我在网上看到了一些能够为2Dquiver
实现这一点的示例,但是它们都不适用于3D变体quiver3
我有下面的图,我想用与它们的大小相对应的颜色来替换蓝色箭头
在旧的图形系统(R2014a及更早版本)中,这不可能使用内置的
quiver
对象。您可以轻松获得用于构成quiver
绘图的所有绘图对象
q = quiver(1:5, 1:5, 1:5, 1:5);
handles = findall(q, 'type', 'line');
但是尾部都由一个plot对象表示,箭头由另一个plot对象表示。因此,您不能单独更改每个头部/尾部的颜色
但是,随着HG2(R2014b及更高版本)的引入,您实际上可以访问两个(未记录的)LineStrip
对象(matlab.graphics.primitive.world.LineStrip
)(一个代表头部,一个代表尾部)。可以通过隐藏属性Tail
和Head
访问这些属性
q = quiver(1, 1, 1, 1);
headLineStrip = q.Head;
tailLineStrip = q.Tail;
然后可以更改这些对象的颜色属性,使每个箭头具有不同的颜色
基本思想
为此,我首先计算所有箭袋箭头的大小(这适用于箭袋
和箭袋3
)
然后,我使用当前颜色贴图将每个幅值映射为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);
LineStrip
ColorData
属性(指定为truecolor
时)还需要有一个alpha通道(我们将其设置为255表示不透明)
此时,我们可以将ColorBinding
属性设置为interpolated
,而不是object
(将其与quiver
对象解耦)并将q.Head
和q.Tail
的ColorData
属性设置为我们上面创建的颜色,使每个箭头都有自己的颜色
全解
注意:此解决方案适用于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).');
并应用于2D箭袋
对象
如果不一定要将箭头缩放到颜色贴图的整个范围,可以使用以下调用histcounts
(而不是上面的行)使用轴的颜色限制来映射大小
clims = num2cell(get(gca, 'clim'));
[~, ~, ind] = histcounts(mags, linspace(clims{:}, size(currentColormap, 1)));
如果您使用的是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)
注意:我没有对震级做任何巧妙的调整,只是根据原始矩阵中的顺序改变每个箭袋的颜色-但是你应该能够了解如何使用这个“功能”
请注意,如果您使用的是Suevers解决方案,并且数据中有NAN,则在调用histcounts之前,应包括此行:
mags(isnan(mags)) = [];
否则,由于matlab不会在U/V/W数据中为NaN创建顶点,您将得到错误输入大小的错误。恐怕您最好修改提交到文件交换以管理3D案例…太棒了!然而,当
U
、V
或W
中存在NAN时,它会失败。这可以通过在调用quiver
/quiver3
之前将所有NaN
替换为零来简单地纠正。
clims = num2cell(get(gca, 'clim'));
[~, ~, ind] = histcounts(mags, linspace(clims{:}, size(currentColormap, 1)));
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)
mags(isnan(mags)) = [];