How to plot index of the coordinate on plot?
16 views (last 30 days)
Show older comments
Kalasagarreddi Kottakota
on 10 Mar 2023
x, y are coordinates and I need also the index of the coordinate. As I have 10 coordinates , I need to have the coordinate number on the plot from 1 to 10.
clear all; close all;
x = linspace(1,10,10);
y=linspace(5,20,10);
plot(x,y,'o')
0 Comments
Accepted Answer
Shubham
on 10 Mar 2023
Edited: Shubham
on 10 Mar 2023
To label each point on the plot with its index number, you can use a for loop to iterate over each coordinate and use the text () function to add the corresponding index number as a label to the plot.
Here's the modified code:
clear all; close all;
x = linspace(1,10,10);
y = linspace(5,20,10);
plot(x, y, 'o')
% add index labels to the plot
for i = 1:length(x)
text(x(i), y(i), num2str(i), 'HorizontalAlignment', 'center')
end
This code should create a plot with each point labeled with its index number, from 1 to 10. The text() function takes the x and y coordinates of the point to label, the label text (which is the index number converted to a string using `num2str()`, and an optional parameter to specify the horizontal alignment of the label (in this case, centered). The loop iterates over each coordinate, adding the corresponding index label to the plot.
0 Comments
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!