How to add space between labels values and its represented values in a plot?

The following code, plots the black dots. I'd like to create some separation between the black dots and the red label number of each dot.
for i=1:nr_BBU
plot(BBU(i,2), BBU(i,1), 'k.', 'MarkerSize',25)
labels = cellstr(num2str([i]'));
%text(BBU(i,2), BBU(i,1), labels, 'Color', 'r', 'HorizontalAlignment', 'left', 'Position', '2')
text(BBU(i,2), BBU(i,1),labels,'HorizontalAlignment','center','VerticalAlignment', 'bottom',...
'Color', 'r', 'FontSize',12)
hold on
end

 Accepted Answer

You can add vertical space by increading the value of the 2nd input to text()
text(BBU(i,2), BBU(i,1) * 1.05,labels, . . .)
% ^^^^^^^^^^^^^^^
or by using labelpoints() from the file exchange. This line below plots the labels North ("N") of the points with a buffer space of 10% (0.1) of the axis range.
labelpoints(BBU(i,2), BBU(i,1), labels, 'N', 0.1, 'Color', 'r', 'FontSize', 12)
% ^^^ buffer

5 Comments

Hi Adam, thanks for your help again!!!
I've tried labelpoints() but only moves the very first label, although it is inside the for loop.
The following modification on the code does the job.
text(BBU(i,2)+0.001, BBU(i,1),labels,'HorizontalAlignment', ...
'center','VerticalAlignment', 'bottom',...
'Color', 'r', 'FontSize',12)
I have a feeling labelpoints() wasn't used correctly but it looks like the text() modification is working for you. I'd be happy to look into what went wrong with the labelpoints method if needed.
I was using the labelpoints() inside the for loop as the code above.
for i=1:nr_BBU
plot(BBU(i,2), BBU(i,1), 'k.', 'MarkerSize',25)
% text labels
labels = cellstr(num2str((i)'));
labelpoints(BBU(i,2), BBU(i,1), labels, 'N', 0.1, 'Color', 'r', 'FontSize', 12)
hold on
end
I don't see why that wouldn't work, assuming nr_BBU is some value larger than 1. It could be simplified to
for i=1:nr_BBU
plot(BBU(i,2), BBU(i,1), 'k.', 'MarkerSize',25)
labelpoints(BBU(i,2), BBU(i,1), i, 'N', 0.1, 'Color', 'r', 'FontSize', 12)
hold on
end
or better yet,
hold on
for i=1:nr_BBU
plot(BBU(i,2), BBU(i,1), 'k.', 'MarkerSize',25)
end
labelpoints(BBU(:,2), BBU(:,1), 1:size(BBU,1), 'N', 0.1, 'Color', 'r', 'FontSize', 12)
Nevertheless, if the text() option is working for you then there's no need for a 3rd party function.
Yes nr_BBU are int, ranging from 1 to 18. Putting the function outside the for loop will just label the last iteration. Nevermind, text() modification is working.
Thanks anyway Adam.

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!