How to plot index of the coordinate on plot?
16 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Shubham
2023-3-10
编辑:Shubham
2023-3-10
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!