散点图如何在Matlab中可视化相同的数据点

6
我可以帮助您进行翻译。以下是需要翻译的内容:

我有一个问题:我需要构建数据的散点图。一切都很好,但是有一些重复的数据:

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];

可以看到有两个元素是 (2, 25); (2,27); (3,24);。如果使用常规的 scatter(x,y) 来构建这个数据,将会丢失这些信息: enter image description here 我找到的解决方法是使用未公开的 'jitter' 参数。
scatter(x,y, 'jitter','on', 'jitterAmount', 0.06);

但是我不喜欢这个外观:

enter image description here

我想要实现的是这样的效果:

enter image description here

其中,当重复数超过1时,数字应该显示在点旁边,或者可能会出现在点内部。

有任何想法如何实现这个效果吗?

1个回答

8
你可以很容易地做到这一点,让我们分成两个部分:
首先,您需要识别唯一的二维点并计数。这就是我们使用uniqueaccumarray函数的原因。如果您不立即理解它们正在做什么以及它们具有哪些输出,请阅读文档:
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 的每一行都包含唯一的二维点,而 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]);

enter image description here

标记内的数字

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

正如您所看到的,我使用散点函数很简单地增大了标记的大小。

enter image description here

颜色编码

scatter(Auniq(:,1), Auniq(:,2), [], cnt);
colormap(jet(max(cnt))); % just for the looks of it

enter image description here

之后,您可以添加颜色条图例来指示每种颜色的发生次数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接