按密度指定箭袋矢量颜色
Specifying quiver vector color by density
我有一些具有定义位置和方向的矢量。我可以使用以下代码在 space 中显示它们:
theta = [pi/2,-pi/2,pi/2,pi/2,pi/2,pi/2,pi/2];
r = 0.25; % magnitude (length) of arrow to plot
x = [4,3.5,3.75,4.5,8,10,12]; y = [8.5,8.2,8.3,8,9,10,8];
u = r * cos(theta); % convert polar (theta,r) to cartesian
v = r * sin(theta);
h = quiver(x,y,u,v,'linewidth',2);
set(gca, 'XLim', [2 15], 'YLim', [4 15]);
从图中可以看出,有些地区的箭头数量比其他地方多。我想按颜色显示箭头,其中每种颜色代表箭头的密度。
有人可以帮我做吗?如果有显示局部密度的连续背景颜色,这也是一个很好的解决方案。
编辑:下面是根据点的密度为绘图背景着色的一些选项。我正在将其编辑到我的答案的顶部,因为它实际上回答了您的问题 - 根据密度分别为 quiver
箭头着色!
x = rand(200,1)*10; y = rand(200,1)*10; % Set up random points
r = 1; u = r * cos(x); v = r * sin(y); % Quiver directions
colormap winter; c = colormap; % Set colourmap and assign to matrix
% Get density of points broken into a 10x10 grid
[n,~,~,binX,binY] = histcounts2(x,y,[10,10]);
% Get colour based on histogram density and chosen colormap colours
col = c(ceil(n(sub2ind(size(n), binX, binY))/max(n(:))*size(c,1)),:);
figure; hold on;
% Each quiver point must be plotted individually (slow!) because colours can
% only be applied to individual quivers. This could be sped up by plotting
% all of the same colour at once.
for ii = 1:size(x,1);
quiver(x(ii),y(ii),u(ii),v(ii),0,'color',col(ii,:));
end
输出:
注意:与下面的例子不同,你不能使用hist3
因为你也需要它returnbin索引。您可以尝试 this File Exchange 函数来获得相同的结果(未经测试)。
这是一个使用 hist3
to get the density (in this example I use a 10x10 grid, as specified when calling hist3
). Then using pcolor
to display the density, and shading interp
来平滑颜色的选项。
注意:hist3
需要 Stats & ML 工具箱,如果您有 Matlab 2015b 或更新版本,则可以改用标准函数 histcounts2(x,y)
.
% Generate points and quiver directions
x = rand(200,1)*10; y = rand(200,1)*10;
u = r * cos(x); v = r * sin(y);
% Get density of points, format for input to pcolor
n = hist3([x,y],[10,10]); % Get density of points broken into a 10x10 grid
colx = linspace(min(x),max(x),size(n,1)+1);
coly = linspace(min(y),max(y),size(n,1)+1);
n = n'; n(size(n,2)+1,size(n,1)+1) = 0;
% Plot
figure
pcolor(colx,coly,n) % Density plot
hold on; colorbar; % Hold on for next plot and show colour bar key
quiver(x,y,u,v,'r') % Quiver plot
shading interp % Smooth plot colours
输出:
编辑:使颜色更柔和
您可以使用 colormap
控制颜色。这可能是默认设置之一,或者您可以创建 RGB 三元组的自定义映射并拥有您想要的任何颜色!下面是一个示例,只需在上面代码的末尾调用 colormap bone;
:
在自定义颜色图中,您可以使颜色更加柔和/对比度更低。
此外,您可以使用 caxis
来缩放绘图的颜色轴!只需调用
caxis([0,2*max(n(:))]);
在上面代码的末尾将最大颜色图值加倍。您可以调整 2
以获得所需的结果:
这看起来没那么花哨,但将箭头颜色指定为 x 轴一定数量的 bin 中箭头数量的函数
close all;
cm=colormap;
theta = [pi/2,-pi/2,pi/2,pi/2,pi/2,pi/2,pi/2];
r = 0.25; % magnitude (length) of arrow to plot
x = [4,3.5,3.75,4.5,8,10,12]; y = [8.5,8.2,8.3,8,9,10,8];
[n,c]=hist(x,5); %count arroes in bins
u = r * cos(theta); % convert polar (theta,r) to cartesian
v = r * sin(theta);
figure;hold on
for ii=1:numel(n) %quiver bin by bin
if n(ii)>0
if ii==1
wx=find(x<(c(ii)+(c(ii+1) - c(ii))/2)); %Which X to plot
elseif ii==numel(n)
wx=find(x>c(numel(n)-1));
else
wx=find((x>(c(ii)-(c(ii)-c(ii-1))/2)).*(x<(c(ii+1)-(c(ii+1)-c(ii))/2)));
end
indCol=ceil( (size(cm,1)*n(ii)-0) / max(n));%color propto density of arrows %in this bin
col = cm(indCol,:);%color for this bin
h = quiver(x(wx),y(wx),u(wx),v(wx),0,'linewidth',2,'color',col);
end
end
colorbar
caxis([0 max(n)])
我有一些具有定义位置和方向的矢量。我可以使用以下代码在 space 中显示它们:
theta = [pi/2,-pi/2,pi/2,pi/2,pi/2,pi/2,pi/2];
r = 0.25; % magnitude (length) of arrow to plot
x = [4,3.5,3.75,4.5,8,10,12]; y = [8.5,8.2,8.3,8,9,10,8];
u = r * cos(theta); % convert polar (theta,r) to cartesian
v = r * sin(theta);
h = quiver(x,y,u,v,'linewidth',2);
set(gca, 'XLim', [2 15], 'YLim', [4 15]);
从图中可以看出,有些地区的箭头数量比其他地方多。我想按颜色显示箭头,其中每种颜色代表箭头的密度。
有人可以帮我做吗?如果有显示局部密度的连续背景颜色,这也是一个很好的解决方案。
编辑:下面是根据点的密度为绘图背景着色的一些选项。我正在将其编辑到我的答案的顶部,因为它实际上回答了您的问题 - 根据密度分别为 quiver
箭头着色!
x = rand(200,1)*10; y = rand(200,1)*10; % Set up random points
r = 1; u = r * cos(x); v = r * sin(y); % Quiver directions
colormap winter; c = colormap; % Set colourmap and assign to matrix
% Get density of points broken into a 10x10 grid
[n,~,~,binX,binY] = histcounts2(x,y,[10,10]);
% Get colour based on histogram density and chosen colormap colours
col = c(ceil(n(sub2ind(size(n), binX, binY))/max(n(:))*size(c,1)),:);
figure; hold on;
% Each quiver point must be plotted individually (slow!) because colours can
% only be applied to individual quivers. This could be sped up by plotting
% all of the same colour at once.
for ii = 1:size(x,1);
quiver(x(ii),y(ii),u(ii),v(ii),0,'color',col(ii,:));
end
输出:
注意:与下面的例子不同,你不能使用hist3
因为你也需要它returnbin索引。您可以尝试 this File Exchange 函数来获得相同的结果(未经测试)。
这是一个使用 hist3
to get the density (in this example I use a 10x10 grid, as specified when calling hist3
). Then using pcolor
to display the density, and shading interp
来平滑颜色的选项。
注意:hist3
需要 Stats & ML 工具箱,如果您有 Matlab 2015b 或更新版本,则可以改用标准函数 histcounts2(x,y)
.
% Generate points and quiver directions
x = rand(200,1)*10; y = rand(200,1)*10;
u = r * cos(x); v = r * sin(y);
% Get density of points, format for input to pcolor
n = hist3([x,y],[10,10]); % Get density of points broken into a 10x10 grid
colx = linspace(min(x),max(x),size(n,1)+1);
coly = linspace(min(y),max(y),size(n,1)+1);
n = n'; n(size(n,2)+1,size(n,1)+1) = 0;
% Plot
figure
pcolor(colx,coly,n) % Density plot
hold on; colorbar; % Hold on for next plot and show colour bar key
quiver(x,y,u,v,'r') % Quiver plot
shading interp % Smooth plot colours
输出:
编辑:使颜色更柔和
您可以使用 colormap
控制颜色。这可能是默认设置之一,或者您可以创建 RGB 三元组的自定义映射并拥有您想要的任何颜色!下面是一个示例,只需在上面代码的末尾调用 colormap bone;
:
在自定义颜色图中,您可以使颜色更加柔和/对比度更低。
此外,您可以使用 caxis
来缩放绘图的颜色轴!只需调用
caxis([0,2*max(n(:))]);
在上面代码的末尾将最大颜色图值加倍。您可以调整 2
以获得所需的结果:
这看起来没那么花哨,但将箭头颜色指定为 x 轴一定数量的 bin 中箭头数量的函数
close all;
cm=colormap;
theta = [pi/2,-pi/2,pi/2,pi/2,pi/2,pi/2,pi/2];
r = 0.25; % magnitude (length) of arrow to plot
x = [4,3.5,3.75,4.5,8,10,12]; y = [8.5,8.2,8.3,8,9,10,8];
[n,c]=hist(x,5); %count arroes in bins
u = r * cos(theta); % convert polar (theta,r) to cartesian
v = r * sin(theta);
figure;hold on
for ii=1:numel(n) %quiver bin by bin
if n(ii)>0
if ii==1
wx=find(x<(c(ii)+(c(ii+1) - c(ii))/2)); %Which X to plot
elseif ii==numel(n)
wx=find(x>c(numel(n)-1));
else
wx=find((x>(c(ii)-(c(ii)-c(ii-1))/2)).*(x<(c(ii+1)-(c(ii+1)-c(ii))/2)));
end
indCol=ceil( (size(cm,1)*n(ii)-0) / max(n));%color propto density of arrows %in this bin
col = cm(indCol,:);%color for this bin
h = quiver(x(wx),y(wx),u(wx),v(wx),0,'linewidth',2,'color',col);
end
end
colorbar
caxis([0 max(n)])