使用matlab在散点图中自动标注

时间:2015-02-21 19:11:48

标签: matlab plot label

所以在我的问题延续不久之前,有了这个建议的程序:

%// Input
A =[
    2   1   5
    1   3   10
    1   -2  5
    0   5   25
    5   0   25
    1   1   2
    -1  -1  2]

[unqcol3,unqidx,allidx] = unique(A(:,3),'stable')
counts = sum(bsxfun(@eq,A(:,3),unqcol3.'),1)  %//'
out =[A(unqidx,:) counts(:)]
scatter(out(:,3), out(:,4))

所以它会生成一个数组和一个这样的图:

     a     b     d     counts
     2     1     5     2
     1     3    10     1
     0     5    25     2
     1     1     2     2

d vs.计数

enter image description here

但是我想把(a,b)作为标签放在每个散点上,所以这就是我想要的情节:

enter image description here

因此你可以看到相应的a,b会在每个散点上自动标注,请帮忙谢谢。

2 个答案:

答案 0 :(得分:1)

我会使用sprintf和一个简单的for循环。

完整代码:

clear
clc


A =[
    2   1   5
    1   3   10
    1   -2  5
    0   5   25
    5   0   25
    1   1   2
    -1  -1  2]

[unqcol3,unqidx,allidx] = unique(A(:,3),'stable');
counts = sum(bsxfun(@eq,A(:,3),unqcol3.'),1);
out =[A(unqidx,:) counts(:)]
scatter(out(:,3), out(:,4));

    %// This step is not necessary but its easier to understand using it.
    a = out(:,1);
    b = out(:,2);
    x = out(:,3);
    y = out(:,4);

for k = 1:size(out,1)

    T{k} = sprintf('%i%i',a(k),b(k))

end
%// Determine x- and y-shift to place text relative to the scatter point
xshift = 0.03; yshift = 0.03;

%// Place the text
text(x+xshift, y+yshift, T);

输出:

enter image description here

答案 1 :(得分:1)

获得out后,您可以使用此no-loop approach -

%// Plot points and set x-y limits
scatter(out(:,3), out(:,4),'o')
xlim([0 30]), ylim([0 2.5])

%// Create string labels
L = cellstr(strcat(strtrim(num2str(out(:,1))),strtrim(num2str(out(:,2)))))

%// Set some y-shift and put text labels
y_shift = 0.1
text(out(:,3),out(:,4)+y_shift,L)
set(gca,'YGrid','on')

代码运行 -

enter image description here

相关问题