使用 matlab 在散点图中自动标记
Auto-Label in scatter plot using matlab
所以继续我刚才的问题,有这个建议的程序:
%// 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 与计数
但我想将 (a,b) 作为每个散点的标签,所以这是我想要的图:
所以你可以看到对应的a,b会自动标注在每个散点上,请帮忙谢谢
我会使用 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);
输出:
有了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')
代码运行-
所以继续我刚才的问题,有这个建议的程序:
%// 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 与计数
但我想将 (a,b) 作为每个散点的标签,所以这是我想要的图:
所以你可以看到对应的a,b会自动标注在每个散点上,请帮忙谢谢
我会使用 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);
输出:
有了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')
代码运行-