
Hovering mouse over multiple plots
26 views (last 30 days)
Show older comments
Hi All
I have multiple subplots, all with the same time stamp.
Is it possible that when i hover over one plot, i can also see the values from another plot at the same time as if i the mouse over that plot as well?
This how my plot looks:

0 Comments
Answers (1)
VINAYAK LUHA
on 22 Sep 2023
Edited: VINAYAK LUHA
on 22 Sep 2023
Hi Dharmesh,
It is my understanding that you have multiple subplots plotted at same timestamps in a figure. While hovering over line points in a subplot, you want to know a workaround to see the corresponding y value in another subplot at the same x values.
Here’s a possible workaround for your reference using callbacks function which gets triggered on hovering over line points and leverages MATLAB Graphics object’s hierarchical relationships to find and display y values as tooltips in the subplot from another configured subplot.
close all
time = 7:16;
data1= randi([1,50],1,10)
data2= randi([51,100],1,10)
figure;
subplot(2, 1, 1);
plot(time, data1);
title('Plot 1');
subplot(2, 1, 2);
plot(time, data2);
title('Plot 2');
dcm_obj = datacursormode(gcf);
set(dcm_obj, 'UpdateFcn',@callbackfcn);
function output_txt = callbackfcn(~, event_obj)
pos = event_obj.Position;
x = pos(1);
% fetch y values from other subplot using child-parent relationship
y1 = interp1(event_obj.Target.XData, event_obj.Target.YData, x);
y2 = interp1(event_obj.Target.Parent.Parent.Children(1).Children.XData, event_obj.Target.Parent.Parent.Children(1).Children.YData, x);
% display data as tooltip
output_txt = {['X: ', num2str(x)], ['Y1: ', num2str(y1)], ['Y2: ', num2str(y2)]};
end
Result

Explore the following documentations for more details:
datacursormode - https://in.mathworks.com/help/matlab/ref/matlab.graphics.shape.internal.datacursormanager.html
Regards
Vinayak Luha
See Also
Categories
Find more on Subplots 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!