Displaying information about the data set by clicking on its plot
50 views (last 30 days)
Show older comments
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!!!!
0 Comments
Accepted Answer
Oleg Komarov
on 15 Jul 2011
% 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))]};
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!