Auto-Label in scatter plot using matlab

好久不见. 提交于 2020-01-07 05:01:11

问题


so in continuation of my problem a while ago, having this suggested program:

%// 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))

so it will generate an array and a plot like this:

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

d vs. counts

but i want to put (a,b) as a labels on each scatter point, so this the plot out put i want:

so as you can see the corresponding a,b will automatically labelled on each scatter points, please help thanks.


回答1:


I would do it using sprintf and a simple for loop.

Complete code:

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

Output:




回答2:


After you have out, you can use this 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')

Code run -



来源:https://stackoverflow.com/questions/28650118/auto-label-in-scatter-plot-using-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!