How do I plot data labels alongside my data in a scatter plot?

조회 수: 4 (최근 30일)
s a
s a 2011년 1월 25일
댓글: Atanu 2022년 3월 15일
I am trying to plot data labels alongside my data in a scatter plot. The goal is to plot several different relationships in the same figure and to include the labels for each of the points.
For example if I have data vectors: [X] and [Y]
and textdata {Xname} to indicate the id corresponding to each (x)
How do I run scatter(X,Y) so that it will also display the string ids [Xname] alongside each data point?

답변 (2개)

Oleg Komarov
Oleg Komarov 2011년 1월 25일

To make it clear with a full example:

% Example input
X = rand(10,1);
Y = rand(10,1);
% example labels
textCell = arrayfun(@(x,y) sprintf('(%3.2f, %3.2f)',x,y),X,Y,'un',0);
% Plot scatter
scatter(X,Y,'filled')
% Add textCell
for ii = 1:numel(X) 
    text(X(ii)+.02, Y(ii)+.02,textCell{ii},'FontSize',8) 
end

Oleg


the cyclist
the cyclist 2011년 1월 25일
I don't know if you can do it within the plotting command, but one way to do this is, after creating the plot, run a loop like
for ii=1:N
text(x(ii),y(ii),textCell{ii})
end
(That code is not exactly right syntactically, but looping with the text command should get you what you want.)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by