Label index/variable name near data points

Hello I,m looking for solution to print a index near to data point:
The example values are:
M =
2278 2 685 2
197 2 2221 2
524 2 2496 2
1061 2 3415 2
651 2 5810 2
1105 2 3817 2
276 2 4527 2
I want to have a text box near to (2278,2) 1; (197,2) 2 .....
lables = 1 %lables = (1:k)
figure('Name','Result','NumberTitle','on');
hold on
p11 = plot(M(:,2),M(:,1),'ro','Color','g');
hold on
p12 = plot(M(:,4),M(:,3),'rx','Color','r');
text(M(:,4),M(:,3),labels,'VerticalAlignment','bottom','HorizontalAlignment','right')
xlabel('tube dimension')
ylabel('Results')
tube_size_help = sprintf('%.0f',tube_size);
nameX=strcat(name ,'_tube_size_',tube_size_help);
saveas(gcf, [nameX '_results.png'])
But I get everythere the same value. Hov can I insert a changing index of row near to M(row,2),M(row,1) (or names of measures)
for loop? Or somethink better?
Thank you!

 Respuesta aceptada

Adam Danz
Adam Danz el 15 de Jun. de 2020
This is easy with labelpoints() found on the file exchange.
figure('Name','Result','NumberTitle','on');
hold on
p11 = plot(M(:,2),M(:,1),'ro','Color','g');
hold on
p12 = plot(M(:,4),M(:,3),'rx','Color','r');
labelpoints(M(:,4),M(:,3), 1:size(M,1), 'E', 0.2)
% "E" means "east" of the coordinate
% 0.2 is a buffer space between the label and the marker

6 comentarios

Nik Rocky
Nik Rocky el 15 de Jun. de 2020
Genius! Thank you very much! :)
Adam Danz
Adam Danz el 15 de Jun. de 2020
Glad I could help!
Nik Rocky
Nik Rocky el 16 de Jun. de 2020
Hello, do you have maybe idea, how to avoid overlapping of values? I'm allready turn TP to east and FP to ost but some values are still overlapping. Thank you!
Adam Danz
Adam Danz el 17 de Jun. de 2020
Editada: Adam Danz el 17 de Jun. de 2020
There is no automated way to detect and reposition overlapping labels but I have thought of this idea and may implement it in the future so be sure to "follow" that file exchange page so you get updates whenever I update the file.
Here's a workaround.
The 5th input below is the "buffer" value (default=0) which determines the space between the (x,y) coordinate and the label. The value is normalized to 10% of the axis limits so a value of 1 will space the label 10% of the axis limit from the label. The documentation describes 'buffer' as a scalar value but it can also be a vector as shown in the example under "idea 1", below.
labelpoints(x,y,'label','E',0.2); % Example
Idea 1: you could shuffle the buffer values for all labels. This example below uses 3 different buffer values and cycles between those values.
x = ones(1,9);
y = rand(1,9);
subplot(3,1,[1,2])
plot(x,y,'o')
% Cycle through 3 buffers to get 9 values
buffer = [.1 .3 .5];
buffer = repmat(buffer,1,ceil(numel(y)/numel(buffer)));
buffer(numel(y)+1:end) = [];
% Sort the buffer values so they match the sorted y values
[~, ySortIdx] = sort(y);
buffer(ySortIdx) = buffer;
% Label points
labelpoints(x,y,1:numel(y),'E',buffer)
Idea 2: you could compute the distance between all pairs of points and somehow use that to determine the buffer value. I haven't thought it through the whole way so there might be some difficult hurdles but it seems doable.
The y-distance between all pairs of points can be computed using
dist = squareform(pdist(y'));
dist(triu(true(size(dist)))) = nan;
Nik Rocky
Nik Rocky el 17 de Jun. de 2020
Thank you very much! I used your first idea - its looks good!
Adam Danz
Adam Danz el 17 de Jun. de 2020
Good! I should also mention that you can click-and-drag the labels after they are plotted, too, in case you need to make final adjustments to a figure you intend to present or publish.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 15 de Jun. de 2020

Editada:

el 17 de Jun. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by