Displaying information about the data set by clicking on its plot

I have a plot of several graphs organized by color. EX:
colors = jet(12);
for i = 1:12
plot(x(:,i),y(:,i),'.','color',colors(i,:))
end
It works well and with the legend, I can display what each plot correlates to: data set 1, data set 2, etc.
However, it would be much better if I could get this information by CLICKING on the data. Such as when you click on a data point using "cursor" you can see the x and y values.
For example, when I click on one of the points, I would like it to say, "Data set 1: X = 12, Y = 3" or however it would display this information. I'm actually more interested in the data set information than the xy position.
Does anyone have any insight as to how to do this or if it's even possible?
Thank you!!!!

 채택된 답변

Oleg Komarov
Oleg Komarov 2011년 7월 15일
% Example figure with 3 datasets, assign tag property when plotting:
colors = jet(3);
x = (1:100).';
y = sin(x)*rand(1,3);
plot(x,y(:,1),'.','color',colors(1,:),'tag',sprintf('dataset %d',1))
hold on
for s = 2:3
plot(x,y(:,s),'.','color',colors(s,:),'tag',sprintf('dataset %d',s))
end
% Then use datacursormode with user-defined UpdateFcn
datacursormode on
dcm = datacursormode(gcf);
set(dcm,'UpdateFcn',@myupdatefcn)
% Save this fcn somewhere on the path
function txt = myupdatefcn(trash,event)
pos = get(event,'Position');
dts = get(event.Target,'Tag');
txt = {dts,...
['X: ',num2str(pos(1))],...
['Y: ',num2str(pos(2))]};

댓글 수: 1

You are awesome! This was exactly what I was looking for!! Thank you times a million!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

질문:

2011년 7월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by