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!!!!

 Akzeptierte Antwort

% 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 Kommentar

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Properties finden Sie in Hilfe-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