gpt4 book ai didi

matlab - 散点图可视化matlab中的相同点

转载 作者:太空宇宙 更新时间:2023-11-03 19:33:43 25 4
gpt4 key购买 nike

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

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),或者可能在点内。

知道如何实现吗?

最佳答案

你可以很容易地做到这一点,让我们把它分成两部分:

首先,您需要识别唯一的 2d 点并计算它们。这就是我们的 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

之后您可以添加 colorbarlegend表示每种颜色出现的次数。

关于matlab - 散点图可视化matlab中的相同点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13778799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com