
Hovering mouse over multiple plots
조회 수: 18 (최근 30일)
이전 댓글 표시
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
답변 (1개)
VINAYAK LUHA
2023년 9월 22일
편집: VINAYAK LUHA
2023년 9월 22일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Axes Appearance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!