How do I label my data points with the corresponding index/x-co​ordinate/y​-coordinat​e?

27 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The following pieces of code will demonstrate two examples of how this might be done.
This example labels the data points with their corresponding x-coordinate values.
% Define x and y
x = (5:30);
y = sin(x);
% Open a new figure
figure;
% Turn hold on so all the points can
% be plotted individually
hold on;
% Calculation for the amount by which the
% label should be displaced in the 'y'
% direction
lbl_dwn = .1*max(y);
% plot and label the individual points
for i = 1:length(x)
plot(x(i),y(i),'r+');
% Label the points with the corresponding 'x' value
text(x(i),y(i)+lbl_dwn,num2str(x(i)));
end
This example labels the data points with the corresponding index of the element.
% Define x and y
x = (5:30);
y = sin(x);
% Open a new figure
figure;
% Turn hold on so all the points can
% be plotted individually
hold on;
% Calculation for the amount by which the
% label should be displaced in the 'y'
% direction
lbl_dwn = .1*max(y);
% plot and label the individual points
for i = 1:length(x)
plot(x(i),y(i),'r+');
% Label the points with the index
text(x(i),y(i)+lbl_dwn,num2str(i));
end

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!