# How to plot index of the coordinate on plot?

14 Ansichten (letzte 30 Tage)
Kalasagarreddi Kottakota am 10 Mär. 2023
Bearbeitet: Shubham am 10 Mär. 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 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Shubham am 10 Mär. 2023
Bearbeitet: Shubham am 10 Mär. 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 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Axis Labels finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by