你可以很容易地做到这一点,让我们分两部分:
首先,您需要确定唯一的2d点并计算它们.这就是我们拥有的unique和accumarray功能.如果您没有立即了解他们正在做什么以及他们有什么输出,请仔细阅读文档:
x = [11 10 3 8 2 6 2 3 3 2 3 2 3 2 2 2 3 3 2 2];
y = [29 14 28 19 25 21 27 15 24 23 23 18 0 26 11 27 23 30 30 25];
A=[x' y'];
[Auniq,~,IC] = unique(A,'rows');
cnt = accumarray(IC,1);
现在Auniq的每一行包含唯一的2d点,而cnt包含每个点的出现次数:
>> [cnt Auniq]
ans =
1 2 11
1 2 18
1 2 23
2 2 25
1 2 26
...etc
为了显示出现的次数,有很多可能性.正如你所提到的,你可以将数字放在散点标记内/旁边,其他选项是颜色编码,标记的大小,……让我们做所有这些,你当然也可以组合!
标记旁边的数字
scatter(Auniq(:,1), Auniq(:,2));
for ii=1:numel(cnt)
if cnt(ii)>1
text(Auniq(ii,1)+0.2,Auniq(ii,2),num2str(cnt(ii)), ...
'HorizontalAlignment','left', ...
'VerticalAlignment','middle', ...
'FontSize', 6);
end
end
xlim([1 11]);ylim([0 30]);
标记内的数字
scatter(Auniq(:,1), Auniq(:,2), (6+2*(cnt>1)).^2); % make the ones where we'll put a number inside a bit bigger
for ii=1:numel(cnt)
if cnt(ii)>1
text(Auniq(ii,1),Auniq(ii,2),num2str(cnt(ii)), ...
'HorizontalAlignment','center', ...
'VerticalAlignment','middle', ...
'FontSize', 6);
end
end
正如您所看到的,我使用散射函数本身非常简单地扩大了标记的大小.
颜色编码
scatter(Auniq(:,1), Auniq(:,2), [], cnt);
colormap(jet(max(cnt))); % just for the looks of it
之后,您可以添加colorbar或legend以指示每种颜色的出现次数.